当前位置:网站首页>Leetcode skimming: binary tree 08 (maximum depth of n-ary tree)
Leetcode skimming: binary tree 08 (maximum depth of n-ary tree)
2022-07-04 04:20:00 【Taotao can't learn English】
559.n The maximum depth of the fork tree
Given a n Fork tree , Find its maximum depth .
Maximum depth is the total number of nodes on the longest path from the root node to the farthest leaf node .
for example , Given a 3 Fork tree :

We should return to its maximum depth ,3.
It's the same , Sequence traversal is enough .
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 The maximum depth of the fork tree * 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>();
// Traverse the current tree hierarchically , Depth of first floor +1
deque.offer(root);
while (!deque.isEmpty()) {
// This sentence is the core , size It is a variable that controls how many elements there are in each layer
int size = deque.size();
maxDepth++;
while (size > 0) {
root = deque.poll();
for (Node item : root.children) {
deque.offer(item);
}
size--;
}
}
return maxDepth;
}
}

边栏推荐
- idea修改主体颜色
- hbuildx中夜神模拟器的配置以及热更新
- Exercises in quantum mechanics
- '2'&gt;' 10'==true? How does JS perform implicit type conversion?
- JDBC 进阶
- Interpretation of leveldb source code skiplist
- Flink learning 8: data consistency
- Katalon使用script实现查询List大小
- 96% of the collected traffic is prevented by bubble mart of cloud hosting
- Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
猜你喜欢

Two commonly used graphics can easily realize data display

Flink learning 7: application structure

02 ls 命令的具体实现

leetcode刷题:二叉树07(二叉树的最大深度)

Penetration practice - sqlserver empowerment

Flink学习8:数据的一致性

PostgreSQL users cannot create table configurations by themselves

96% of the collected traffic is prevented by bubble mart of cloud hosting

【CSRF-01】跨站请求伪造漏洞基础原理及攻防

Pointer array and array pointer
随机推荐
(指針)自己寫一個比較字符串大小的函數,功能與strcmp類似。
如何有效远程办公之我见 | 社区征文
Understand the principle of bytecode enhancement technology through the jvm-sandbox source code
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
网络 - VXLAN
[Logitech] m720
JDBC advanced
02 specific implementation of LS command
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
Flink学习7:应用程序结构
Perf simple process for multithreaded profile
软件测试是干什么的 发现缺陷错误,提高软件的质量
STM32 external DHT11 display temperature and humidity
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Support the first triggered go ticker
Katalon中控件的参数化
Redis cluster uses Lua script. Lua script can also be used for different slots
Small record of thinking