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.

For example, given a 3-ary tree:

We should return its max depth, which is 3.

Note:

  1. The depth of the tree is at most 1000.
  2. The total number of nodes is at most 5000.

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

解法一:

class Solution {
public:
int maxDepth(Node* root) {
int res = ;
helper(root, , 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 + , res);
}
}
};

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

解法二:

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

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

解法三:

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

类似题目:

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 题目讲解汇总(持续更新中...)

[LeetCode] Maximum Depth of N-ary Tree N叉树的最大深度的更多相关文章

  1. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  2. LeetCode:Maximum Depth of Binary Tree_104

    LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...

  3. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. Leetcode Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. [Leetcode] Maximum depth of binary tree二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  7. LeetCode Maximum Depth of Binary Tree (求树的深度)

    题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...

  8. leetcode Maximum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  9. leetcode:Maximum Depth of Binary Tree【Python版】

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

随机推荐

  1. spring的纯注解的IOC配置

    package config; import com.mchange.v2.c3p0.ComboPooledDataSource;import org.apache.commons.dbutils.Q ...

  2. ZH奶酪:Ubuntu 14.04安装LAMP(Linux,Apache,MySQL,PHP)

    (Linux Operating System,Apache Web Server,MySQL database,PHP) 首先,一个三行命令搞定的方法: sudo apt-get update su ...

  3. torch.utils.data.DataLoader对象中的迭代操作

    关于迭代器等概念参考:https://www.cnblogs.com/zf-blog/p/10613533.html 关于pytorch中的DataLoader类参考:https://blog.csd ...

  4. js设置document.domain实现跨域

    document.domain 只能实现跨子域的问题 如:xxx.com/a.html 和aaa.xxx.com/b.html 或:bbb,xxx.com/c.html 和ccc.xxx.com/d. ...

  5. mysql 客户端连接报错Illegal mix of collations for operation

    服务端用的是utf-8,客户端工具打开表和视图是会报Illegal mix of collations for operation错误,经排查,可以采用以下语句解决 SET character_set ...

  6. 【原创】大叔经验分享(6)Oozie如何查看提交到Yarn上的任务日志

    通过oozie job id可以查看流程详细信息,命令如下: oozie job -info 0012077-180830142722522-oozie-hado-W 流程详细信息如下: Job ID ...

  7. 五种ip proxy的设置方法

    我们在制作爬虫爬取想要的资料时,由于是计算机自动抓取,强度大.速度快,通常会给网站服务器带来巨大压力,所以同一个IP反复爬取同一个网页,就很可能被封,在这里介绍相关的技巧,以免被封:但在制作爬虫时,还 ...

  8. vue-i18n国际化在data中切换不起作用

    vue-i18n是一个针对于vue的国际化插件,使用非常简单,具体使用方式看我细细道来. 实现方式 1. 下载包 npm install vue-i18n 2. 配置 在main.js文件中加入如下配 ...

  9. java----JDOM解析XML

    JDOM: 与DOM类似,基于树形结构 效率比DOM快 下载: http://www.jdom.org/dist/binary/jdom-2.0.6.zip 导包导java中的工程目录 jdom-2. ...

  10. mac os安装shell man中文帮助工具(manpages-zh),即man命令显示中文帮助文档

    一.从官网(http://pkgs.fedoraproject.org/repo/pkgs/man-pages-zh-CN)下载安装包:或者从开源代码(https://github.com/man-p ...