当前位置:首页 » 《随便一记》 » 正文

leetcode每日一题-559:N叉树的最大深度_苦泉的博客

19 人参与  2022年05月24日 11:43  分类 : 《随便一记》  评论

点击全文阅读


leetcode每日一题-559:N叉树的最大深度

链接

N 叉树的最大深度


题目

在这里插入图片描述



分析

简单的搜索题目。只需要从根节点开始dfs一下整个N叉树就可以得到答案了。主要是对dfs要理解和掌握N叉树的遍历。



代码

C++

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:

    int res = 0;

    int maxDepth(Node* root) {
        if(root == nullptr) return res;
        dfs(root, 1);
        return res;
    }

    void dfs(Node* root, int deep)
    {
        res = max(res, deep);

        for(auto ve : root->children)
        {
            dfs(ve, deep + 1);
        }
    }
};

Java

class Solution {
    public int maxDepth(Node root) {
        if (root == null) {
            return 0;
        }
        int maxChildDepth = 0;
        List<Node> children = root.children;
        for (Node child : children) {
            int childDepth = maxDepth(child);
            maxChildDepth = Math.max(maxChildDepth, childDepth);
        }
        return maxChildDepth + 1;
    }
}

作者:LeetCode-Solution

JavaScript

var maxDepth = function(root) {
    if (!root) {
        return 0;
    }
    let maxChildDepth = 0;
    const children = root.children;
    for (const child of children) {
        const childDepth = maxDepth(child);
        maxChildDepth = Math.max(maxChildDepth, childDepth);
    }
    return maxChildDepth + 1;
};

作者:LeetCode-Solution

点击全文阅读


本文链接:http://zhangshiyu.com/post/40673.html

深度  题目  作者  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 重生八零:妹妹替我嫁给养猪万元户免费_[妹妹张志强家珍]小说章节试读
  • 宋南乔贺景川宋南乔贺景川免费
  • 错轨七年,得知真相后他疯了后续在线阅读_慕行渊小雨乔青璃节选高光片段速递‌
  • 万般风月解语花最新章节在线阅读_裴延江婉婉婉后续在线阅读
  • 「我们才刚答案,你成武圣了?」精彩章节试读_「杨间苏沐晴」精彩节选免费试读
  • (番外)+(全书)沈昭宜傅沉砚(填我十万八千梦+后续+番外)完结_(沈昭宜傅沉砚)列表_笔趣阁(填我十万八千梦+后续+番外)
  • (番外)+(全书)腐草为萤不相念全书+后续+结局(江心璐裴纪臣)全书免费在线_(腐草为萤不相念全书+后续+结局)列表_笔趣阁(江心璐裴纪臣)
  • 孟卿卿谢昭远绮萝(孟卿卿谢昭远绮萝结局+番外)_(孟卿卿谢昭远绮萝)列表_笔趣阁(孟卿卿谢昭远绮萝结局+番外)
  • 孤舟不渡我节选名场面直通车‌_傅斯屿沈静姝程晏淮完结版免费在线阅读
  • 今日热读(跟恶毒女配互换身体后,她哭惨了+后续+结局)沈思允谢景行_跟恶毒女配互换身体后,她哭惨了+后续+结局热点全书在线(沈思允谢景行)
  • 你是我难愈的旧伤+后续+结局(傅深铭夏晚星)_(傅深铭夏晚星)你是我难愈的旧伤+后续+结局列表_笔趣阁(傅深铭夏晚星)
  • 「匿爱成殇」完结_[苏晴顾言黄脸婆]精彩章节试读

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1