当前位置:网站首页>leetcode刷题:二叉树08(N叉树的最大深度)
leetcode刷题:二叉树08(N叉树的最大深度)
2022-07-04 03:51:00 【涛涛英语学不进去】
559.n叉树的最大深度
给定一个 n 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
例如,给定一个 3叉树 :

我们应返回其最大深度,3。
其实一样的,层序遍历即可。
package com.programmercarl.tree;
import java.util.ArrayDeque;
import java.util.Deque;
/** * @ClassName MaxDepth2 * @Descriotion TODO * @Author nitaotao * @Date 2022/7/3 22:24 * @Version 1.0 * 559. N 叉树的最大深度 * https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/ **/
public class MaxDepth2 {
public int maxDepth(Node root) {
int maxDepth = 0;
if (root == null) {
return maxDepth;
}
Deque<Node> deque = new ArrayDeque<Node>();
//层次遍历当前树,一层深度+1
deque.offer(root);
while (!deque.isEmpty()) {
//这句话是核心, size是控制每层有多少元素的变量
int size = deque.size();
maxDepth++;
while (size > 0) {
root = deque.poll();
for (Node item : root.children) {
deque.offer(item);
}
size--;
}
}
return maxDepth;
}
}

边栏推荐
- Getting started with the go language is simple: go implements the Caesar password
- [webrtc] M98 Ninja build and compile instructions
- 1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
- STM32外接DHT11显示温湿度
- Pandora IOT development board learning (HAL Library) - Experiment 6 independent watchdog experiment (learning notes)
- 如何远程办公更有效率 | 社区征文
- 还原窗口位置的微妙之处
- 图解网络:什么是热备份路由器协议HSRP?
- Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
- JS实现文字滚动 跑马灯效果
猜你喜欢

干货!基于GAN的稀有样本生成
![CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构](/img/ba/c1d40de154344ccc9f2fd1dd4cb12f.png)
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构

Es network layer

10 reasons for not choosing to use free virtual hosts

Two commonly used graphics can easily realize data display

分布式系统:what、why、how

The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO

Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)

渗透实战-SQLServer提权

三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
随机推荐
[paddleseg source code reading] paddleseg calculation dice
新型数据中心,助力加快构建以数据为关键要素的数字经济
STM32 external DHT11 display temperature and humidity
智慧地铁| 云计算为城市地铁交通注入智慧
MySQL one master multiple slaves + linear replication
Smart subway | cloud computing injects wisdom into urban subway transportation
Support the first triggered go ticker
STM32外接DHT11显示温湿度
Two commonly used graphics can easily realize data display
1289_FreeRTOS中vTaskSuspend()接口实现分析
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
ctf-pikachu-CSRF
疫情来袭--远程办公之思考|社区征文
Parameterization of controls in katalon
Detailed explanation of PPTC self recovery fuse
毕业总结
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
Go 语言入门很简单:Go 实现凯撒密码
vue多级路由嵌套怎么动态缓存组件
EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?