如果二叉树既没有左子树也没有右子树,深度为1。
其余情况下二叉树的深度等于左右子树中较大的深度加上1。
// 获取二叉树的深度
int GetBiTreeDepth(BTNode* pRoot)
{
if (NULL == pRoot)
return 0;
if (NULL == pRoot->pLeft && NULL == pRoot->pRight)
return 1;
int leftDepth = GetBiTreeDepth(pRoot->pLeft);
int rightDepth = GetBiTreeDepth(pRoot->pRight);
return (leftDepth > rightDepth) ? leftDepth + 1 : rightDepth + 1;
}