[LeetCode] 559. Maximum Depth of N-ary Tree N叉树的最大深度

 

Given a n-ary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

 

Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: 3

Example 2:

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: 5

 

Constraints:

  • The total number of nodes is in the range [0, 104].
  • The depth of the n-ary tree is less than or equal to 1000.

 

这道题让求一个N叉树的最大深度,由于之前做过 Maximum Depth of Binary Tree 那道题,所以有了求二叉树的基础后,求N叉树也就不难了,无非就是稍稍变换了一下吗,由固定的左右子结点变成了一个一堆子结点,但是方法还是没有变。首先来看一种常见的递归解法,就是需要有一个当前深度,然后带一个全局变量 res 进去。在递归函数中,如果 node 为空,直接返回。若子结点数组为空,那么结果 res 和 cur 比较取较大值。否则就遍历子结点数组,对每个子结点调用递归函数,这里的 cur 要自增1,参见代码如下:

 

解法一:

复制代码
class Solution {
public:
    int maxDepth(Node* root) {
        int res = 0;
        helper(root, 1, res);
        return res;
    }
    void helper(Node* node, int cur, int& res) {
        if (!node) return;
        if (node->children.empty()) res = max(res, cur);
        for (Node* child : node->children) {
            helper(child, cur + 1, res);
        }
    }
};
复制代码

 

我们也可以不使用其他的函数,直接主函数中递归,首先判空,否则就是遍历子结点数组,然后对每个子结点调用递归函数的返回值加1后跟 res 相比,取较大值更新结果res,参见代码如下:

 

解法二:

复制代码
class Solution {
public:
    int maxDepth(Node* root) {
        if (!root) return 0;
        int res = 1;
        for (Node* child : root->children) {
            res = max(res, maxDepth(child) + 1);
        }
        return res;
    }
};
复制代码

 

我们也可以不使用递归,而是用迭代的形式,这里借助队列 queue 来做,就是 BFS 的经典写法,不算难,参见代码如下:

 

解法三:

复制代码
class Solution {
public:
    int maxDepth(Node* root) {
        if (!root) return 0;
        int res = 0;
        queue<Node*> q{{root}};
        while (!q.empty()) {
            for (int i = q.size(); i > 0; --i) {
                auto t = q.front(); q.pop();
                for (auto child : t->children) {
                    if (child) q.push(child);
                }
            }
            ++res;
        }
        return res;
    }
}; 
复制代码

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/559

 

类似题目:

Maximum Depth of Binary Tree

 

参考资料:

https://leetcode.com/problems/maximum-depth-of-n-ary-tree/

https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/148544/Java-Top-down-DFS-solutions

https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/167010/DFS-and-BFS-solutions-in-C%2B%2B.

https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/151804/Solution-Python-C%2B%2B-Simple-with-explanation

 

LeetCode All in One 题目讲解汇总(持续更新中...)

posted @   Grandyang  阅读(3528)  评论(3编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· 开发的设计和重构,为开发效率服务
· 从零开始开发一个 MCP Server!
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
历史上的今天:
2017-10-29 [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和
2016-10-29 [LeetCode] All O`one Data Structure 全O(1)的数据结构
2015-10-29 [CareerCup] 13.1 Print Last K Lines 打印最后K行
2014-10-29 [LeetCode] 88. Merge Sorted Array 合并两个有序数组
Fork me on GitHub

喜欢请打赏

扫描二维码打赏

Venmo 打赏

点击右上角即可分享
微信分享提示