输入一颗二叉树的跟节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
答案:
int TreeDepth(BinaryTreeNode* pRoot)
{
if(pRoot==nullptr)
return 0;
int nLeft=TreeDepth(pRoot->m_pLeft);
int nRight=TreeDepth(pRoot->m_pRight);
return (nLeft>nRight)?(nLeft+1):(nRight+1);
}