您的位置:首页 > 其它

leedcode做题总结,题目Minimum Depth of Binary Tree 2012/10/09

2014-07-15 16:02 387 查看
找二叉树深度的最小值。和求树高差不多,注意的是递归中不能和求树高一样直接返回两个子树的最小值,因为如果一个节点只有一个子树求两边的最小值会出错,所以需要判断下。

public int minDepth(TreeNode root) {
if(root==null) return 0;
else{
if(root.right==null) return minDepth(root.left)+1;
if(root.left==null) return minDepth(root.right)+1;
return Math.min(minDepth(root.right)+1,minDepth(root.left)+1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: