首页 > 其他 > 详细

leadcode的Hot100系列--104. 二叉树的最大深度

时间:2019-07-01 23:30:59      阅读:104      评论:0      收藏:0      [点我收藏+]

依然使用递归思想。
思路:
1、树的深度 = max (左子树深度,右子树深度)+ 1 。 ------> 这里的加1是表示自己节点深度为1。
2、如果当前节点为null,则说明它的左右子树深度为0。

int max(int a, int b)
{
    if (a>b)
        return a;
    else
        return b;
}

int maxDepth(struct TreeNode* root){
    int iDepth = 0;

    if (NULL == root)
        return 0;
    
    iDepth = max(maxDepth(root->left), maxDepth(root->right)) + 1;
    return iDepth;
}

leadcode的Hot100系列--104. 二叉树的最大深度

原文:https://www.cnblogs.com/payapa/p/11117059.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!