实现代码:
int GetBinaryTreeDepth(BinaryTreeNode* pRoot)
{
if (nullptr == pRoot) return 0;
//int leftTreeDepth = GetBinaryTreeDepth(pRoot->pLeft);
//int rightTreeDepth = GetBinaryTreeDepth(pRoot->pRight);
//相比上面的写法,下面各自少一个递归调用
int leftTreeDepth = pRoot->pLeft != nullptr ? GetBinaryTreeDepth(pRoot->pLeft) : 0;
int rightTreeDepth = pRoot->pRight != nullptr ? GetBinaryTreeDepth(pRoot->pRight) : 0;
return (leftTreeDepth > rightTreeDepth) ? (leftTreeDepth + 1) : (rightTreeDepth + 1);
}