[LeetCode] 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.
For example, given a 3-ary
tree:
We should return its max depth, which is 3.
Note:
- The depth of the tree is at most
1000
. - 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;
}
};
类似题目:
参考资料:
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
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Maximum Depth of N-ary Tree N叉树的最大深度的更多相关文章
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- LeetCode:Maximum Depth of Binary Tree_104
LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
- leetcode Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode:Maximum Depth of Binary Tree【Python版】
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
随机推荐
- Exponent CMS 2.3.9 配置文件写入 Getshell分析
在 install/index.php 的第44行 expString::sanitize($_REQUEST); 跟进sanitize函数 public static function saniti ...
- [物理学与PDEs]第3章第5节 一维磁流体力学方程组 5.2 一维磁流体力学方程组的 Lagrange 形式
由 $$\bex \cfrac{\p \rho}{\p t}&+u_1\cfrac{\p \rho}{\p x}+\rho\cfrac{\p u_1}{\p x}=0, \eex$$ 我们可以 ...
- Resources (being shared)
论文下载求助论坛 数学杂志的模版 答辩PPT模版 发一篇文章的经历 数学期刊名称缩写 英文书籍下载 英文书籍下载 中文书籍下载 数学分析高等代数考研试题官方下载地址
- LATEX简易教程
1.LaTeX软件的安装和使用方法A(自助):在MikTeX的官网下载免费的MikTeX编译包并安装.下载WinEdt(收费)或TexMaker(免费)等编辑界面软件并安装.方法B(打包):在ctex ...
- 通过IDEA搭建scala开发环境开发spark应用程序
一.idea社区版安装scala插件 因为idea默认不支持scala开发环境,所以当需要使用idea搭建scala开发环境时,首先需要安装scala插件,具体安装办法如下. 1.打开idea,点击c ...
- 嵌套调用less函数时参数值的变化及提取部分-遁地龙卷风
在a.less中导入base.lessa.less中的代码 .animate-ripple-ink{ .animation(ripple .5s linear;{ 100%{ opacity:; .t ...
- pythonのdjango连接MYSQL
在py3.*中利用django使用mysql时,会出现一些问题.由于django默认的是 MySQLdb,但MySQLdb目前不支持py3.*所以我们要改用pymysql,首先要安装pymysql,命 ...
- C#学习笔记-域用户认证(一)
public Boolean ValidateDomainUser(string Domain, string UserName, string Password) { DirectoryEntry ...
- vue 动态变量值不变化
caseData = { lists:[] }; vm = new Vue({ el: '.hs-mt', data: caseData }); function getlist(pid,id){ $ ...
- mysql只删除表不删除库
1.备份数据库mysqldump -uroot -P13306 -h10.1.50.14 -p 库名 > /opt/20190109.sql2.删除所有表mysql -uroot -P13306 ...