思路:树的深度 = 左右子树最大的深度 + 1
code:
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root)
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
return 0;
}
};
本文深入探讨了如何使用递归方法计算二叉树的深度,通过实例代码演示了算法实现过程,帮助开发者掌握二叉树深度计算的核心技巧。
思路:树的深度 = 左右子树最大的深度 + 1
code:
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root)
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
return 0;
}
};
被折叠的 条评论
为什么被折叠?