3.30
本来以为只要左子树或者是右子树为空 直接返回1 就可以了,事实并不是这样的
有的时候真的是太想当然了
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int minDepth(TreeNode root) {
if(root == null){
return 0;
}
//int min = 0;
if(root.right == null){
return minDepth(root.left)+1;
}
if(root.left == null){
return minDepth(root.right)+1;
}
int x1 = minDepth(root.left) + 1;
int x2 = minDepth(root.right) + 1;
return x1<x2?x1:x2;// write your code here
}
}
本文探讨了一个关于二叉树最小深度计算的问题,并通过具体的Java代码实现了解决方案。作者最初认为仅当左子树或右子树为空时直接返回1即可,但发现实际情况更为复杂。文章详细介绍了如何正确地递归计算二叉树的最小深度。
311

被折叠的 条评论
为什么被折叠?



