当前位置:网站首页>111. Minimum depth of binary tree
111. Minimum depth of binary tree
2022-07-03 12:13:00 【zwanying】
Force link 111. Minimum depth of binary tree
Given a binary tree , Find out the minimum depth .
The minimum depth is the number of nodes on the shortest path from the root node to the nearest leaf node .
explain : A leaf node is a node that has no children .
Example 1:
Input :root = [3,9,20,null,null,15,7]
Output :2
Example 2:
Input :root = [2,null,3,null,4,null,5,null,6]
Output :5
Tips :
The number of nodes in the tree ranges from [0, 105] Inside
-1000 <= Node.val <= 1000
Their thinking
Sequence traversal , If there are leaf nodes in this layer , The current number of layers is the minimum depth .
Realization
class Solution {
public int minDepth(TreeNode root) {
// Sequence traversal iteration
Queue<TreeNode> queue = new LinkedList<>();
int deep = 0;
if(root == null){
return deep;
}
queue.offer(root);
int flag = 0;
while(!queue.isEmpty()){
deep++;
int len = queue.size();
while(len-->0){
TreeNode t = queue.poll();
if(t.left == null && t.right == null){
flag =1;
break;
}
if(t.left != null) queue.offer(t.left);
if(t.right != null) queue.offer(t.right);
}
if(flag == 1) break;
}
return deep;
}
}
边栏推荐
- Cacti monitors redis implementation process
- Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
- Shardingsphere sub database and sub table < 3 >
- Shell: basic learning
- Vulnhub geminiinc
- vulnhub之pyexp
- Introduction to the implementation principle of rxjs observable filter operator
- 【mysql官方文档】死锁
- win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
- Raven2 of vulnhub
猜你喜欢
PHP導出word方法(一mht)
实现验证码验证
CGroup introduction
Develop plug-ins for idea
Talk about the state management mechanism in Flink framework
Niuniu's team competition
QT OpenGL texture map
Symlink(): solution to protocol error in PHP artisan storage:link on win10
PHP导出word方法(一phpword)
[official MySQL document] deadlock
随机推荐
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
QT OpenGL rotate, pan, zoom
Itext7 uses iexternalsignature container for signature and signature verification
vulnhub之pyexp
Pragma pack syntax and usage
Integer int compare size
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
Concurrent programming - singleton
Shutter: overview of shutter architecture (excerpt)
Basic knowledge of OpenGL (sort it out according to your own understanding)
Fluent: Engine Architecture
vulnhub之Nagini
Dart: self study system
安装electron失败的解决办法
STL tutorial 8-map
Flutter Widget : Flow
New features of ES6
OpenGL index cache object EBO and lineweight mode
Visual studio 2022 downloading and configuring opencv4.5.5
LeetCode 0556.下一个更大元素 III - 4步讲完