当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
Understand the principle of bytecode enhancement technology through the jvm-sandbox source code
The difference between bagging and boosting in machine learning
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
Flink learning 7: application structure
Flink学习8:数据的一致性
How to telecommute more efficiently | community essay solicitation
leetcode刷题:二叉树04(二叉树的层序遍历)
Perf simple process for multithreaded profile
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
mysql数据库的存储
随机推荐
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
Flink学习8:数据的一致性
VIM add interval annotation correctly
深度优先搜索简要讲解(附带基础题)
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
如何有效远程办公之我见 | 社区征文
量子力学习题
Katalon使用script实现查询List大小
[book club issue 13] multimedia processing tool ffmpeg tool set
网络 - VXLAN
Storage of MySQL database
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
还原窗口位置的微妙之处
My opinion on how to effectively telecommute | community essay solicitation
Parameterization of controls in katalon
10 reasons for not choosing to use free virtual hosts
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
Brief explanation of depth first search (with basic questions)
函数计算异步任务能力介绍 - 任务触发去重
思考的小记录