设为首页 加入收藏

TOP

leadcode的Hot100系列--104. 二叉树的最大深度
2019-07-02 14:12:42 】 浏览:103
Tags:leadcode Hot100 系列 --104. 最大 深度

依然使用递归思想。
思路:
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;
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇结构体的传参理解成员的存储方式 下一篇linux 广播和组播

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目