[Leetcode 559]N叉树的最大深度Maximum Depth of N-ary Tree DFS/BFS模板
题目
https://leetcode.com/problems/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
思路
可以用BFS/DFS
BFS核心模板
Queue q=new LinkedList
q.add(初始点)
while(!q.isempty){
q=q.size();
for(int i=0;i<q;i++){
cur=q.remove() 提出当前元素
广度优先遍历,不断加进新元素(q不为空时一直在while,为空时证明已经遍历完成)
q.add(初始点附近符合要求的节点)
}
这里写遍历过程中想记录更改什么
}
代码
BFS
public int maxDepth(Node root) {
//bfs
if (root==null)
return 0;
int depth=0;
Queue<Node> q=new LinkedList<>();
q.offer(root);//加入初始点
while(!q.isEmpty()){
int size=q.size();
for(int i=0;i<size;i++){
Node curNode=q.remove();//提取出当前节点
for(Node child:curNode.children){
q.offer(child);//新加入满足条件的点
}
}
depth++;
}
return depth;
}
DFS
int max_depth=0;
public int maxDepth(Node root) {
dfs(root,1);
return max_depth;
} public void dfs(Node node,int curDepth){
if (node==null)
return; max_depth=Math.max(max_depth,curDepth); for (Node child:node.children){
dfs(child,curDepth+1);//每次depth+1
} }
[Leetcode 559]N叉树的最大深度Maximum Depth of N-ary Tree DFS/BFS模板的更多相关文章
- Java实现 LeetCode 559 N叉树的最大深度(遍历树,其实和便利二叉树一样,代码简短(●ˇ∀ˇ●))
559. N叉树的最大深度 给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度,3. 说明: 树的深度不 ...
- Leetcode 559. N叉树的最大深度
题目链接 https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/description/ 题目描述 给定一个N叉树,找到其最大深度. ...
- Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)
Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- LeetCode:N叉树的最大深度【559】
LeetCode:N叉树的最大深度[559] 题目描述 给定一个N叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度, ...
- Leetcode:559. N叉树的最大深度
Leetcode:559. N叉树的最大深度 Leetcode:559. N叉树的最大深度 Talk is cheap . Show me the code . /* // Definition fo ...
- [LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...
- [Swift]LeetCode104. 二叉树的最大深度 | 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] 104. Maximum Depth of Binary Tree_Easy tag: DFS
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 559. N叉树的最大深度
给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度,3. 说明: 树的深度不会超过 1000. 树的节点 ...
随机推荐
- Java DelayQueue包装类
public class DelayQueueWrapper<T> { private TimeUnit timeUnit; private final Long capacity; pr ...
- 自定义注解+反射提取对象到map中
一.问题:有时候我们与第三方接口对接传参时,需要将对象里的字段和值以map形式传给别人,此时可以借助其他的工具类,但是我个人用起来不太灵活,还会把多余的字段传给别人,因此我们自己动手搞一套 二.思路: ...
- 解决Vue.js devtools插件成功装上,却在控制台中找不到的问题
参考:https://blog.csdn.net/CCCCt1/article/details/97668483
- PyMySQL创建
title: PyMySQL创建 author: 杨晓东 permalink: PyMySQL创建 date: 2021-10-02 11:27:04 categories: - 投篮 tags: - ...
- CF652F 题解
题意 传送门 在一个长度为 \(m\) 的圆环上有 \(n\) 只初始位置互不相同的蚂蚁,每只蚂蚁的速度都为 \(1\),初始方向为顺时针或逆时针:两只运动方向不同的蚂蚁相遇时会调转方向,问 \(t\ ...
- 查看Linux操作系统版本命令
(一)查看操作系统版本的方法 1.uname -a 可以查看内核版本等信息 Linux test 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:4 ...
- AD中添加原理图文档模板-转载
(24条消息) AD中添加原理图文档模板_shuguang552的博客-CSDN博客_ad原理图模板
- js - console
js - console 参考资料 JavaScript Console 对象 Node.js console.debug()用法及代码示例 nodejs.org console.log输出字体颜色 ...
- js生成快速生成0~n的数组
Object.keys(Array.from(Array(100)))
- create-react-app卸载与升级
用create-react-app 创建项目发现报错 You are running `create-react-app` 4.0.3, which is behind the latest rele ...