题目

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. 8. semahpore原理

    一.上游服务比下游服务抗压能力应该更强一些,因为直接面对的是前端.Semphore控制访问特定资源的线程数目.实际场景可用于限流.在hystrix里面用了. 另:ReadWriteLock的作用是什么 ...

  2. 使用nvm安装不同版本的NodeJS

    下载及安装 下载地址:https://github.com/coreybutler/nvm-windows/releases 配置nodejs的镜像地址 nvm node_mirror https:/ ...

  3. vm虚拟机和主机之间互传文件

    u盘大家都有吧,用u盘吧,超方便! vmtools 从主机传文件到虚拟机 可以通过之间复制粘贴/拖拽 或者共享文件夹的方式 从虚拟机传文件到主机,查到了说说是要在虚拟机里面改一个什么映射设置,改完之后 ...

  4. Docker内容总结

    Docker内容总结目录什么是Docker?Docker的应用场景有哪些?Docker的优点有哪些?Docker与虚拟机的区别是什么?Docker的三大核心是什么?如何快速安装Docker?如何修改D ...

  5. Flutter 中的普通路由、普通路由传值、 命名路由、命名路由传值

    一.Flutter 中的路由 Flutter 中的路由通俗的讲就是页面跳转.在 Flutter 中通过 Navigator 组件管理路由导航.并提供了管理堆栈的方法.如:Navigator.push ...

  6. Python3 学习基础知识

    python是动态语言(对象属性可以动态改变,删除添加..),不是强类型语言,所以和java,c/c++等强类型静态语言有不一样地方需要注意. 一:基本数据类型 变量 counter = 1   # ...

  7. ES6新增运算符 ?? || &&

    运算符(?? || &&) && 与运算符 &&左边表达式为真时执行右边表达式 let a = true let b = 0 a && ...

  8. vue data functions should return an object

    报错: 原因:data里没写return{}

  9. Java基础——(综合练习)普通加密

    package com.zhao.test; public class Test18 { /*需求: ​ 某系统的数字密码(大于0),比如1983, 采用加密方式进行传输. 规则如下: ​ 先得到每位 ...

  10. CF1557总结

    CF1557总结 Codeforces Round #737 (Div. 2) 先看了 A .意思是要把序列分成两个子序列,使得两序列各自平均值的和最小,输出最小值,要求 \(O(n)\) .想半天然 ...