当前位置:网站首页>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;
}
}
边栏推荐
- Kubernetes three dozen probes and probe mode
- STL tutorial 8-map
- PHP get the file list and folder list under the folder
- Redis
- Unity3d learning notes 5 - create sub mesh
- PHP导出word方法(一phpword)
- Why can't my MySQL container start
- (構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
- previous permutation lintcode51
- Qt OpenGL 纹理贴图
猜你喜欢
随机推荐
php 获取文件夹下面的文件列表和文件夹列表
Momentum of vulnhub
How to convert a numeric string to an integer
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
Optimize interface performance
Colleagues wrote a responsibility chain model, with countless bugs
Oracle advanced (I) realize DMP by expdp impdp command
Redis notes 01: Introduction
Swagger
网络通讯之Socket-Tcp(一)
Flutter: about monitoring on flutter applications
Dart: About zone
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
(数据库提权——Redis)Redis未授权访问漏洞总结
Fluent: Engine Architecture
Slf4j log facade
Socket TCP for network communication (I)
MCDF Experiment 1
vulnhub之momentum









![[official MySQL document] deadlock](/img/2d/04e97d696f20c2524701888ea9cd10.png)