题目描述
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
求树的深度,可以从层次遍历和深度遍历角度考虑!
思路1:递归,注意边界条件
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
int count = 0;
int max = 0;
dfs(pRoot, max, count);
return max;
}
void dfs(TreeNode* pRoot, int& max,int& count) {
//深度遍历
if (pRoot == nullptr)