[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. 树的节点 ...
随机推荐
- luna lunatic
Luna是罗马神话的月神.英语中Lunacy.Lunatic等意指疯狂的字语源均来自Luna.月亮的阴晴圆缺影响地球的潮汐涨退甚至生物周期,故此古时的人们相信月亮拥有使人疯狂的魔力,人狼等传说亦是因此 ...
- .netcore webapi的返回值和过滤器
1.返回值. 1.1直接返回数据 1.2 数据+状态码 返回这种类型IActionResult 可以使用return OK(T).return NotFound(T) 1.3前两种的混合使用Actio ...
- tsc条码打印机如何导入表格批量打印
很多时候,我们在TSC条码打印机的权昌条码打印软件里做标签时,涉及数据特别大,都保存在EXCLE表格里,那要怎么做,才可以使软件批量打印EXCEL数据呢?下面,小编就教教大家一种简单的批量打印标签的方 ...
- java技术系列(二) 反射
能够分析类能力的程序称为反射. 反射能力: 在运行中分析类的能力. 在运行中查看对象,例如:编写一个toString方法供所有类使用. 实现通用的数组操作代码. 利用Method对象.
- 增、改生产订单组件BAPI BAPI_ALM_ORDER_MAINTAIN
转载留存 IT_METHODS LIKE BAPI_ALM_ORDER_METHOD处理方法,必选项,存储CREATE CREATETONOTIF CHANGE DELETE RELEAS ...
- (一)REDIS之常见数据结构及操作
(一)基本数据结构 1.1 String结构: String底层结构是动态字符串,可修改指定位置数据,通过预分配冗余空间减少内存的频繁分配,实际分配的空间capacity一般要高于实际字符串长度len ...
- PaddleOcr-noavx离线部署文档
PaddleOcr-noavx离线部署文档 环境与版本说明: 系统 架构 Anaconda3 PaddlePaddle PaccleOCR 银河麒麟Server V10 X86 Anaconda3-2 ...
- ABAP 范围表 range table
范围表定义: DATA gr_test TYPE RANGE OF char6. 做选择屏幕的时候 范围选择框 默认就是一个范围表 范围表内容: 通过断点调试可以看到,范围表有4列 sign opti ...
- 创建一个本地CocoaPods库 并在项目中使用该库
1.新建一个项目如下 2.往TestLib中添加两个文件 3.终端进入TestLib 生成git文件 然后提交到本地 git init git add . git commit -m '添加perso ...
- java 图片BASE64 转字节流 byte[]
String base64Url = base64UrlArray.get(0)+"";BASE64Decoder decoder = new BASE64Decoder();re ...