求解二叉树的深度是多少?
二叉树的深度
http://www.nowcoder.com/questionTerminal/435fb86331474282a3499955f0a41e8b
链接:https://www.nowcoder.com/questionTerminal/435fb86331474282a3499955f0a41e8b?toCommentId=4022030
来源:牛客网
ben求一颗二叉树的深度是多少,说到二叉树或者树,一般都是会运用递归来解决。像这道题就是一道非常基础的递归调用,只要对语法熟悉,就可以快速实现,在本题中我用到了math函数,所以需要加入相关的头文件,代码如下:
#include<math.h>
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(pRoot == NULL){
return 0;
}
else{
int left = TreeDepth(pRoot -> left);
int right = TreeDepth(pRoot -> right);
if(left >= right){
return left+1;
}
else {
return right+1;
}
}
}
};


