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. LFYZ-OJ ID: 1009 阶乘和

    思路 循环n次,每次计算i的阶乘i!,并加入sum中. n的范围从1~100,这里一定要使用高精度运算,涉及到"高精度乘低精度","高精度加高精度". 避免每次 ...

  2. C#(在WeBAPI)获取Oracle(在PL/SQL)游标类型的存储过程(用到了RefCursor)

    需求:WebAPI服务端,通过Oracle数据库的存储过程,获取数据. 在PL/SQL 建立存储过程:(先来最简单的,就是把整个表都查出来) create or replace procedure S ...

  3. Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  4. EF6 CodeFirst使用MySql

    如何使用EF CodeFirst连接MySql数据库? 环境:VS2015.Win7..NetFramework4.5.2.MySql5.6 一.基本操作 1.创建MVC5项目:ZmsoftsWebM ...

  5. Python基础【第二篇】

    一.Python的标准数据类型 Python 3中主要有以下6中数据类型: Number(数字).String(字符串).List(列表).Tuple(元组).Sets(集合).Dictionary( ...

  6. 浅析Memcache和Redis

    想必开发的小伙伴们对Memcache和Redis都不陌生吧,最近正好在整理它们,于是就写一下博客吧!一方面是分享,另一方面便于自己查找. 首先,来说说Memcache和Redis是什么? 说得简单一点 ...

  7. docker安装 <一>

    一.docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制, ...

  8. HTTPS开发(SSL--用Tomcat服务器配置https双向认证)

    准备工作: 1.windows+R  cmd 打开命令窗口 2.输入:cd C:\Program Files\Java\jdk1.7.0_80\bin 进入路径找到keytool工具 为服务器生成证书 ...

  9. Cesium 动态更换模型贴图方法

    参考资料 github 讨论地址 示例代码地址 示例代码 var viewer = new Cesium.Viewer('cesiumContainer'); var scene = viewer.s ...

  10. 一起学HBase——简单介绍HBase各种组件

    HBase是谷歌BigTble的开源实现.谷歌的三篇论文拉开了大数据江湖的序幕,铸就了现在以Hadoop为主的大数据技术生态圈.而HBase是开源的大数据数据库,和传统的行式数据库不同的是,HBase ...