class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
}
104.二叉树的最大深度
递归计算二叉树最大深度
最新推荐文章于 2024-09-14 17:16:46 发布
该博客主要介绍了如何使用递归算法计算二叉树的最大深度。通过`maxDepth`函数,分别计算左子树和右子树的最大深度,然后取较大值加1得到整棵树的最大深度。
477

被折叠的 条评论
为什么被折叠?



