/*递归法。某树的深度为其左右子树深度较大者加1.*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == nullptr) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};LeetCode之Maximum Depth of Binary Tree
最新推荐文章于 2024-10-15 15:32:42 发布
本文介绍了一种计算二叉树最大深度的递归算法。通过递归地计算左子树和右子树的最大深度,并选取较大者加一得到整棵树的深度。
129

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



