155 - 二叉树的最小深度

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


你可能感兴趣的:(lintcode)