当前位置:网站首页>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;
}
}
边栏推荐
- postgresql 用户不能自己创建表格配置
- Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
- 2020 Bioinformatics | TransformerCPI
- mysql数据库的存储
- ctf-pikachu-CSRF
- How to add custom API objects in kubernetes (1)
- [Yugong series] go teaching course 002 go language environment installation in July 2022
- [Logitech] m720
- Graduation project: design seckill e-commerce system
- How to dynamically cache components in Vue multi-level route nesting
猜你喜欢
Flink learning 8: data consistency
Tcpclientdemo for TCP protocol interaction
Activiti7 task service - process variables (setvariable and setvariablelocal)
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
Getting started with the go language is simple: go implements the Caesar password
How to telecommute more efficiently | community essay solicitation
Idea modify body color
Evolution of MySQL database architecture
图解网络:什么是热备份路由器协议HSRP?
laravel admin里百度编辑器自定义路径和文件名
随机推荐
postgresql 用户不能自己创建表格配置
JS realizes the effect of text scrolling marquee
User defined path and file name of Baidu editor in laravel admin
(pointeur) Écrivez - vous une fonction qui compare la taille de la chaîne et fonctionne comme strcmp.
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
Msgraphmailbag - search only driveitems of file types
Flink learning 6: programming model
Evolution of MySQL database architecture
Redis cluster uses Lua script. Lua script can also be used for different slots
Three years of graduation, half a year of distance | community essay solicitation
Support the first triggered go ticker
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
The difference between bagging and boosting in machine learning
DP83848+网线热拔插
Rhcsa-- day one
RHCSA 08 - automount配置
线程常用的方法
软件测试是干什么的 发现缺陷错误,提高软件的质量
Katalon使用script实现查询List大小
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure