111. Minimum Depth of Binary Tree

左右子树选短的,但是注意如果没有子树不能选0,一开始做错一次。。

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

    }
}
posted @ 2016-11-05 12:38  哇呀呀..生气啦~  阅读(88)  评论(0)    收藏  举报