剑指offer----二叉树的深度

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

//递归调用,很简单,要用非递归的话,用queue实现,带上一个层数即可
class Solution {
public:
    int depth(TreeNode* root,int nowDepth)
    {
        if(root==NULL)return nowDepth;
        return max(depth(root->left,nowDepth+1),depth(root->right,nowDepth+1));
    }
    int TreeDepth(TreeNode* pRoot)
    {
        return depth(pRoot,0);
    }
};

猜你喜欢

转载自blog.csdn.net/xiaocongcxc/article/details/82766119