最小二叉树的深度
给出一个二叉树,找出其最小的深度
思路:这个题和最大的二叉树深度有所不同,因为二叉树的最大深度只需将左子树和右子树的深度进行最大值的比较即可,但是求其最小深度时需要注意如果直接进行最小深度比较的话会返回0,会干扰题的判断,因此判断的条件也会多了一些。
public class Solution {
public int run(TreeNode root) {
if (root == null){
return 0;
}
if (root.left == null && root.right == null){
return 1;
}
if (root.left == null){
return run(root.right) + 1;
}
else if (root.right == null){
return run(root.left) + 1;
}
else {
return 1 + Math.min(run(root.left),run(root.right));
}
}
}