给一个dfs的题解

二叉树的深度

http://www.nowcoder.com/questionTerminal/435fb86331474282a3499955f0a41e8b

void dfs(TreeNode* pRoot,int& res,int&dep){
         ++dep;
        if(pRoot->left==nullptr&&pRoot->right==nullptr){res=max(res,dep);--dep;return;}
        if(pRoot->left) dfs(pRoot->left,res,dep);
        if(pRoot->right)dfs(pRoot->right,res,dep);
        --dep;
    }
    int TreeDepth(TreeNode* pRoot)
    {
         int res=0,dep=0;
        if(pRoot==nullptr)return res;
        dfs(pRoot,res,dep);
        return res;

    }
全部评论

相关推荐

04-03 09:32
已编辑
华南农业大学 golang
我的代码出BUG了:"晚点发个邮件调整一下时间",你收到新的邮件没,如果没有收到新的邮件,那就需要进入面试链接留痕,否则系统会判定你迟到
点赞 评论 收藏
分享
03-29 18:59
运城学院 Java
程序员小白条:咱们要对自己的简历和学历有清晰的认知,不要动不动就大厂了....都26届了,没实习还想着大厂,唉
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务