- [二叉树]111. Minimum Depth of Binar
- 二叉树的最小、最大深度以及平衡二叉树
- leetcode:111. Minimum Depth of B
- 111. Minimum Depth of Binary Tre
- 111. Minimum Depth of Binary Tre
- 111. Minimum Depth of Binary Tre
- 111. Minimum Depth of Binary Tre
- 111. Minimum Depth of Binary Tre
- 111. Minimum Depth of Binary Tre
- 111. Minimum Depth of Binary Tre
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
![](https://img.haomeiwen.com/i1560080/3d58c25acd0a7649.png)
(图片来源https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
)
日期 | 是否一次通过 | comment |
---|---|---|
2020-03-15 | 0 |
NOTE: [1,2] ,返回2
![](https://img.haomeiwen.com/i1560080/d1e51030b3e1f8b3.png)
递归
public int minDepth(TreeNode root) {
if(root == null) {
return 0;
}
int leftD = minDepth(root.left);
int rightD = minDepth(root.right);
// 防止corner case: [1,2], 返回应该是2,而不是1,所以需要leftD+rightD+1
return (leftD == 0 || rightD == 0) ? leftD+rightD+1 : Math.min(leftD, rightD) + 1;
}
网友评论