/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(!pRoot)
return 0;
return 1+max(TreeDepth(pRoot->left),TreeDepth(pRoot->right));
}
};
本文介绍了一种计算二叉树深度的方法,通过递归遍历左子树和右子树,返回最大深度加一。使用C++实现了一个名为Solution的类,其中包含TreeDepth函数,该函数接收一个指向二叉树根节点的指针作为参数。
3822

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



