題目描述
輸入一棵二叉樹,求該樹的深度。從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度為樹的深度。
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(pRoot == NULL)
return 0;
int left,right;
if(pRoot->left==NULL)
left = 0x80000000;
if(pRoot->right==NULL)
right = 0x80000000;
left = TreeDepth(pRoot->left);
right = TreeDepth(pRoot->right);
return left > right ? left + 1 : right + 1;
}
};