当前位置:网站首页>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;
}
}

边栏推荐
- Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
- [Yugong series] go teaching course 002 go language environment installation in July 2022
- 1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
- 深入浅出对话系统——使用Transformer进行文本分类
- Calculate the odd sum of 1~n (1~100 as an example)
- AAAI2022 | Word Embeddings via Causal Inference: Gender Bias Reducing and Semantic Information Preserving
- [book club issue 13] packaging format of video files
- PostgreSQL users cannot create table configurations by themselves
- Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
- Class summation, shortest row
猜你喜欢

functools下的reduce函数

The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
![[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability](/img/46/cb5a10ffe3fcdffb7da68dbaef5b1f.png)
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability

Katalon framework test web (XXVI) automatic email

mysql数据库的存储

Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool

图解网络:什么是热备份路由器协议HSRP?

Objective-C description method and type method
![CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构](/img/ba/c1d40de154344ccc9f2fd1dd4cb12f.png)
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构

Flink学习8:数据的一致性
随机推荐
量子力学习题
毕业三年,远程半年 | 社区征文
[PaddleSeg 源码阅读] PaddleSeg 自定义数据类
Deep thinking on investment
"Implement both software and hardware" to help build a new cloud computing data center
02 specific implementation of LS command
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
STM32 external DHT11 display temperature and humidity
10 reasons for not choosing to use free virtual hosts
还原窗口位置的微妙之处
MySQL one master multiple slaves + linear replication
01 qemu 启动编译好的镜像 VFS: Unable to mount root fs on unknown-block(0,0)
Brief explanation of depth first search (with basic questions)
PostgreSQL users cannot create table configurations by themselves
毕业总结
渗透实战-SQLServer提权
Unity 绘制弹球和台球的运动轨迹
SQL statement strengthening exercise (MySQL 8.0 as an example)
Es network layer