题目描述
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
递归
class Solution
{
public:
int TreeDepth(TreeNode *
pRoot)
{
if (pRoot ==
NULL)
return 0;
int depLeft =
1 + TreeDepth(pRoot->
left);
int depRight =
1 + TreeDepth(pRoot->
right);
return max(depLeft, depRight);
}
};
转载于:https://www.cnblogs.com/ruoh3kou/p/10147657.html