【LeetCode】 111. Minimum Depth of Binary Tree
来源:互联网 发布:淘宝举报假冒及盗版 编辑:程序博客网 时间:2024/05/16 06:30
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = minDepth(root.left);
int rightHeight = minDepth(root.right);
if (leftHeight == 0) {
return rightHeight + 1;
}
if (rightHeight == 0) {
return leftHeight + 1;
}
return Math.min(leftHeight, rightHeight) + 1;
}
}
- LeetCode 111. Minimum Depth of Binary Tree
- [LeetCode]111.Minimum Depth of Binary Tree
- 【LeetCode】111.Minimum Depth of Binary Tree
- [Leetcode] 111. Minimum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- 【LeetCode】111. Minimum Depth of Binary Tree
- LeetCode *** 111. Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- leetcode-111. Minimum Depth of Binary Tree
- LeetCode-111.Minimum Depth of Binary Tree
- LeetCode 111.Minimum Depth of Binary Tree
- Leetcode 111. Minimum Depth of Binary Tree
- [leetcode]111.Minimum Depth of Binary Tree
- 111.[Leetcode]Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- 【leetcode】111. Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- 用源码解释为什么在Android Studio中Button的文本是大写
- Leetcode Two Sum II - Input array is sorted
- Leetcode 27. Remove Element
- 【LeetCode】 110. Balanced Binary Tree
- 161220
- 【LeetCode】 111. Minimum Depth of Binary Tree
- JavaEE中的MVC(五)定制Struts——Action跳转JSP
- React-Native从入门到放弃(一)准备篇
- React-Native从入门到放弃(二)
- Leetcode 118. Pascal's Triangle
- [C++]Leetcode #8 atoi()
- date命令--Linux命令应用大词典729个命令解读
- compress命令--Linux命令应用大词典729个命令解读
- vgscan命令--Linux命令应用大词典729个命令解读