当前位置:网站首页>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;
}
}
边栏推荐
- Integer string int mutual conversion
- Symlink(): solution to protocol error in PHP artisan storage:link on win10
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- typeScript
- Slf4j log facade
- Test classification in openstack
- (数据库提权——Redis)Redis未授权访问漏洞总结
- Unity3d learning notes 5 - create sub mesh
- 4000字超详解指针
- 023(【模板】最小生成树)(最小生成树)
猜你喜欢
随机推荐
DNS multi-point deployment IP anycast+bgp actual combat analysis
安装electron失败的解决办法
"Jianzhi offer 04" two-dimensional array search
OpenGL 着色器使用
Interview experience in summer camp of Central South University in 2022
【mysql官方文档】死锁
Integer int compare size
XML (DTD, XML parsing, XML modeling)
20. Valid brackets
Experience container in libvirt
vulnhub之pyexp
225. Implement stack with queue
Flutter: self study system
Visual studio 2022 downloading and configuring opencv4.5.5
Solve msvcp120d DLL and msvcr120d DLL missing
Flutter Widget : Flow
Go language to realize static server
typeScript
Pragma pack syntax and usage
vulnhub之tomato(西红柿)








