[剑指offer] 38. 二叉树的深度

题目描述

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

递归
class Solution
{
  public:
    int TreeDepth(TreeNode *pRoot)
    {
        if (pRoot == NULL)
            return 0;
        int depLeft = 1 + TreeDepth(pRoot->left);
        int depRight = 1 + TreeDepth(pRoot->right);
        return max(depLeft, depRight);
    }
};

猜你喜欢

转载自www.cnblogs.com/ruoh3kou/p/10147657.html