题目

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模板的更多相关文章

  1. Java实现 LeetCode 559 N叉树的最大深度(遍历树,其实和便利二叉树一样,代码简短(●ˇ∀ˇ●))

    559. N叉树的最大深度 给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度,3. 说明: 树的深度不 ...

  2. Leetcode 559. N叉树的最大深度

    题目链接 https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/description/ 题目描述 给定一个N叉树,找到其最大深度. ...

  3. Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)

    Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...

  4. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

  5. LeetCode:N叉树的最大深度【559】

    LeetCode:N叉树的最大深度[559] 题目描述 给定一个N叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度, ...

  6. Leetcode:559. N叉树的最大深度

    Leetcode:559. N叉树的最大深度 Leetcode:559. N叉树的最大深度 Talk is cheap . Show me the code . /* // Definition fo ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

  10. 559. N叉树的最大深度

    给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度,3. 说明: 树的深度不会超过 1000. 树的节点 ...

随机推荐

  1. spring security 基于角色的控制,可运行。

    基于角色的访问控制 建表语句,见 上一篇 的博文,  https://www.cnblogs.com/sdgtxuyong/p/16157870.html 在配置类中,@EnableGlobalMet ...

  2. 对VC中有关数据类型转换的整理

    原文地址:http://spaces.msn.com/wsycqyz/blog/cns!F27CB74CE9ADA6E7!152.trak 对VC中有关数据类型转换的整理   说明:本文纯粹是总结一下 ...

  3. 栈和寄存器虚拟机比较(以python和lua为例)

    指令长度 python python的指令定长,长度为16bit,其中8bit操作码,8bit操作数. ///@file: Python-3.6.0\Include\code.h typedef ui ...

  4. js中常用的运算符

    1. ?. 链接运算符 特性: 一旦遇到空置就会终止 例子: let name = obj?.name persion.getTip?.() // 没有getTip 方法则不会执行 2. ?? 空值合 ...

  5. 刚开始学python不知从何学习推荐你一本《Python零基础入门》书,免费领取

    百度云盘:Python零基础入门学习pdf高清版书籍下载 提取码:yzh3        

  6. Java数组之数组的使用

    数组的使用 普通的for循环 For-Each循环 数组作方法入参 数组作返回值 public class ArrayDemo03 { public static void main(String[] ...

  7. 攻防世界Web进阶篇——warmup

    打开链接,发现是一张滑稽 查看页面源代码,发现文件 于是打开source.php,发现 打开hint.php,根据提示得知flag在ffffllllaaaagggg文件中 回到source.php,检 ...

  8. docker 清除redis缓存

    1.仓库容器id: docker  ps 2.进入容器:docker exec -it 容器id  redis-cli 3.清除所有缓存:flushall

  9. 链式前向星+dijkstra

    https://leetcode-cn.com/problems/network-delay-time/submissions/ // n <= 100 class Solution { int ...

  10. D. Steps to One

    题意 初始有一个空数组\(a\),接下来每次操作会这么做: 在\([1,n]\)中选择一个数,将其拼接在数组\(a\)后. 计算数组\(a\)的\(\gcd\). 如果结果是\(1\),退出. 否则, ...