[剑指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);
    }
};

 

posted @ 2018-12-20 10:00  Ruohua3kou  阅读(98)  评论(0)    收藏  举报