(leetcode)二叉树最大深度

个人主页:Lei宝啊

愿所有美好如期而遇


目录

题目:

思路:

代码:

图解:


题目:

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。


思路:

记录左树深度,记录右树深度,哪个大取哪个。


代码:

复制代码
nt maxDepth(struct TreeNode* root)
{
    
    if(root == NULL)
        return 0;

    int ldepth = maxDepth(root->left);
    int rdepth = maxDepth(root->right);
   
    return ldepth > rdepth ? ldepth + 1 : rdepth + 1; 
}

图解:


相关推荐
老师我太想进步了20262 小时前
【数据结构-8】栈的实现
c语言·数据结构·算法
Pniubi6 小时前
力扣19 . 删除表的倒数第N个节点(栈)
算法·leetcode·职场和发展
文丑颜不良啊21 小时前
LeetCode2761~2780题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2841~2860题目解析
java·leetcode
【心态好不摆烂】1 天前
数据结构基础精讲3
数据结构·算法
岑梓铭1 天前
《考研408数据结构》第七章(B树 和 B+树)复习笔记
数据结构·笔记·b树·考研·408
文丑颜不良啊1 天前
LeetCode2921~2940题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2901~2920题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2861~2880题目解析
java·leetcode
岑梓铭1 天前
《考研408数据结构》第八章(排序)复习笔记
数据结构·笔记