[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. 树的节点 ...
随机推荐
- C++的右值引用是左值,rvalue reference is lvalue.
参考: https://stackoverflow.com/questions/28483250/rvalue-reference-is-treated-as-an-lvalue
- CDO学习1 CDO简介
参考自如下网站 http://www.ceda.ac.uk/static/media/uploads/ncas-reading-2015/cdo.pdf 介绍 一个有几百种操作符的单独命令 CDO受N ...
- CSS 多栏布局 固定布局 流动布局
正常情况下都应该保持元素 height 属性的默认值 auto . 多栏布局,某一栏目占的总宽度包括它的,Width,margin,padding ,border. CSS3中,应用 box-siz ...
- Python——01.环境及安装
Python介绍 -- Python是解释型,面向对象的语言,程序结构简洁,清晰 -- Python解释器分类: CPython(官方解释器):用C语言编写的Python解释器 PyPy:用Pytho ...
- 用户警告:“importlib-metadata”版本与“setuptools”不兼容。升级importlib-metadata
Warning: `importlib-metadata` version is incompatible with `setuptools` 解决方案:升级 importlib-metadata 版 ...
- 通过EXCEL/WPS文件,拼接SQL,刷数据库数据
WPS如何把日期变成文本格式? [快捷选择同一列多条记录]同一列,鼠标左键标记A,SHIFT+鼠标左键标记B ,等于选择A-B的之间的数据 [向下填充]在第一行输入数据,选择同一列A-B的之间的数据, ...
- mariadb数据库查询(select)
查询基本使用(条件,排序,聚合函数,分组,分页) 示例:--创建学生表 create table students ( id int unsigned not null auto_increment ...
- 【C学习笔记】day2-2 不允许创建临时变量,交换两个数的内容(附加题)
#include<stdio.h> int main() { int a=0, b=1; int m[2]; m[0] = a; m[1] = b; a = m[1]; b = m[0]; ...
- linux创建数据库以及数据库用户密码
登录linux服务器成功后: 登录mysql: mysql -uroot -p 输入密码:xxxx 创建数据库: create database test 创建用户及密码: create user ' ...
- nodejs redis执行lua脚本
const Redis = require("ioredis"); const redis = new Redis({ port: 6300, // Redis port host ...