码迷,mamicode.com
首页 > 其他好文 > 详细

求二叉树最大最小深度

时间:2017-10-14 21:51:17      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:false   ==   class   深度   java   node   treenode   turn   highlight   

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);
    }

  

求二叉树最大最小深度

标签:false   ==   class   深度   java   node   treenode   turn   highlight   

原文地址:http://www.cnblogs.com/youza/p/7668599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!