您的位置:首页 > 其它

二叉树的深度

2015-06-11 17:33 253 查看
时间限制:1秒空间限制:32768K
通过比例:56.94%
最佳记录:0ms|8552K(来自 牛客688826号


题目描述

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

/*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;
int maxdepth=0;
LNRSearch(pRoot,1,maxdepth);
return maxdepth;
}
void LNRSearch(TreeNode *root,int depth,int& max)
{
if(!root) return;
LNRSearch(root->left,depth+1,max);
if(depth>max) max=depth;
LNRSearch(root->right,depth+1,max);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: