题目

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. CSS中的选择( ::selection和user-select)

      CSS中的选择( ::selection和user-select) 在网络上,我们出于不同原因选择内容,也许我们想复制文本并在某处引用它.对于移动端来说,选择内容比较难,我不喜欢在移动端选择内容. ...

  2. win10+py38环境分分钟装好geopandas

    python版本是anaconda自带3.8,尝试了下面这个网上最推荐的安装方法 conda install --channel conda-forge geopandas 但多次以失败告终,看了几个 ...

  3. vue IE9兼容flex布局 css3(转载)

    原文 https://blog.csdn.net/shihezhengshz/article/details/118860562 写这文章的时候的我,心力憔悴鸭,找了好长时间,呜呜┭┮﹏┭┮ 好了,开 ...

  4. Java基于springboot大学生宿舍寝室考勤人脸识别管理系统

    简介 Java基于springboot开发的大学生寝室管理系统宿舍管理系统.学生可以查找寝室和室友信息,可以申请换寝室,申请维修,寝室长提交考勤信息(宿管确认学生考勤信息),补签,查看寝室通报,宿管信 ...

  5. 整合log4j

    引入依赖 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId& ...

  6. Mysql数据库基础第三章:DML语言

    Mysql数据库基础系列 软件下载地址 提取码:7v7u 数据下载地址 提取码:e6p9 mysql数据库基础第一章:(一)数据库基本概念 mysql数据库基础第一章:(二)mysql环境搭建 mys ...

  7. Django 知识点总结

    知识点总结 一.URL: 1.在python 正则表达式中,正则表达式命名组的语法是(?P<name>pattern),其中命名组中的命名就是name,并且pattern 是某些匹配的模式 ...

  8. Nginx安装以及部署Django项目

    Nginx官网:http://nginx.org/ Nginx中文文档:https://www.nginx.cn/doc/index.html Tengine(淘宝Nginx):http://teng ...

  9. 手写简单call、apply、bind

    1.call ~function(){ function call_1(context, ...args){ context = context == undefined ? window : con ...

  10. node.js 新手快速入门

    我当初学的时候,是在大大们的指导下开始学习的,用了3天搞定大大们给的任务.下面我就把这个经历分享出来,让大家借鉴一下.欢迎吐槽~~ 任务如下: 根据Node js 开发入门教程第五章的一个使用node ...