给定一个二叉树,求此二叉树的深度。
递归方法:
int TreeDepth(TreeNode* pRoot)
{
if (pRoot == NULL)
return 0;
if (pRoot->left == NULL && pRoot->right == NULL)
return 1;
return max(TreeDepth(pRoot->left), TreeDepth(pRoot->right)) + 1;
}int TreeDep给定一个二叉树,求此二叉树的深度。
递归方法:
int TreeDepth(TreeNode* pRoot)
{
if (pRoot == NULL)
return 0;
if (pRoot->left == NULL && pRoot->right == NULL)
return 1;
return max(TreeDepth(pRoot->left), TreeDepth(pRoot->right)) + 1;
}int TreeDep