柚子da

导航

 

1.求二叉树最大深度

public int maxDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
    }

2.求二叉树最小深度

int minDepth(TreeNode *root) {
        if(root == NULL)
            return false;

        if(root->left == NULL) return minDepth(root->right) + 1;
        if(root->right == NULL) return minDepth(root->left) + 1;

        int leftDepth = minDepth(root->left);
        int rightDepth = minDepth(root->right);

        return leftDepth < rightDepth ? (leftDepth + 1) : (rightDepth + 1);
    }

  

posted on 2017-10-14 21:19  柚子da  阅读(106)  评论(0)    收藏  举报