当前位置:网站首页>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;
}
}
边栏推荐
- Shutter: overview of shutter architecture (excerpt)
- Vulnhub's cereal
- How to deploy web pages to Alibaba cloud
- 使用BLoC 构建 Flutter的页面实例
- Vulnhub pyexp
- During FTP login, the error "530 login incorrect.login failed" is reported
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- Dart: view the dill compiled code file
- 安装electron失败的解决办法
- Cacti monitors redis implementation process
猜你喜欢

CGroup introduction

Ripper of vulnhub

Shutter: overview of shutter architecture (excerpt)

Vulnhub geminiinc

(construction notes) ADT and OOP

PHP export word method (phpword)

PHP export word method (one MHT)

Why can't my MySQL container start

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

STL tutorial 10 container commonalities and usage scenarios
随机推荐
Shutter: add gradient stroke to font
Vulnhub's presidential
Vulnhub narak
MySQL uses the method of updating linked tables with update
CGroup introduction
DEJA_ Vu3d - cesium feature set 053 underground mode effect
STL tutorial 10 container commonalities and usage scenarios
Optimize interface performance
Wechat applet - basic content
4000字超详解指针
PHP get the file list and folder list under the folder
XML (DTD, XML parsing, XML modeling)
Download address and installation tutorial of vs2015
Wrong arrangement (lottery, email)
Pragma pack syntax and usage
Kubernetes three dozen probes and probe mode
Shutter widget: centerslice attribute
Master and backup role election strategy in kept
Unity3d learning notes 5 - create sub mesh
vulnhub之Nagini