[leetcode] 111. 二叉树的最小深度

111. 二叉树的最小深度

与获取最大深度不同的是,要注意判断下是不是叶子节点,

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return getMin(root);
    }
    
    public int getMin(TreeNode root){
        if (root == null) {
            return Integer.MAX_VALUE;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return Math.min(getMin(root.left), getMin(root.right)) + 1;
    }
}

猜你喜欢

转载自www.cnblogs.com/acbingo/p/9917398.html