(java)Minimum Depth of Binary Tree
来源:互联网 发布:妃子 侍寝 知乎 编辑:程序博客网 时间:2024/05/22 10:28
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.
思路:也是穷举法的方法,每次都取最小的深度。当root==null时 return 0;
当root.left!=null || root.right!=null时,return 1+min(minDep(root.left),minDep(root.right));
当root.left或者root.right有一个为空时,返回1+minDep(root.left)+minDep(root.right);
代码如下(已通过leetcode)
public class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
else {
if(root.left!=null && root.right!=null ) return 1+Math.min(minDepth(root.left), minDepth(root.right));
else return 1+minDepth(root.left)+minDepth(root.right);
}
}
}
0 0
- [Leetcode] Minimum Depth of Binary Tree (Java)
- Minimum Depth of Binary Tree Java
- Minimum Depth of Binary Tree (Java)
- Minimum Depth of Binary Tree Java
- [LeetCode][Java] Minimum Depth of Binary Tree
- LeetCode|Minimum Depth of Binary Tree-java
- (java)Minimum Depth of Binary Tree
- leetcode:Minimum Depth of Binary Tree 【Java】
- minimum-depth-of-binary-tree java code
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- TwoStep Cluster
- Python自省(反射)指南
- UICollectionView的使用
- 3D Touch APIs
- 创建者模式
- (java)Minimum Depth of Binary Tree
- 当ubuntu已有gcc4时安装gcc3
- Android UiAutomator编译与运行测试代码
- fastclick导致的iphone中select点击闪退的bug
- 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
- QGIS算法
- ImageView的scaleType属性
- StringBuffer类型
- 入门——ci _remap的用法