当前位置:网站首页>Leetcode skimming: binary tree 09 (minimum depth of binary tree)
Leetcode skimming: binary tree 09 (minimum depth of binary tree)
2022-07-04 04:20:00 【Taotao can't learn English】
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 :
Given binary tree [3,9,20,null,null,15,7],

Return its minimum depth 2.
Still use sequence traversal , When this layer is dissatisfied , And !!! A leaf node appears , Then the minimum depth appears .
package com.programmercarl.tree;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
/** * @ClassName MinDepth * @Descriotion TODO * @Author nitaotao * @Date 2022/7/3 22:32 * @Version 1.0 * 111. Minimum depth of binary tree * https://leetcode.cn/problems/minimum-depth-of-binary-tree/ **/
public class MinDepth {
public int minDepth(TreeNode root) {
// Sequence traversal , Which line is dissatisfied , Default , Is the shortest depth
int minDepth = 0;
if (root == null) {
return minDepth;
}
Deque<TreeNode> deque = new ArrayDeque<TreeNode>();
deque.offer(root);
int length = 1;
while (!deque.isEmpty()) {
int size = deque.size();
boolean flag = false;
while (size > 0) {
root = deque.poll();
// Leaf nodes appear
if (root.left == null && root.right == null) {
flag = true;
}
if (root.left != null) {
deque.offer(root.left);
}
if (root.right != null) {
deque.offer(root.right);
}
size--;
}
length *= 2;
minDepth++;
if (deque.size() != length && flag) {
return minDepth;
}
}
return minDepth;
}
}

边栏推荐
- Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
- "Implement both software and hardware" to help build a new cloud computing data center
- Pytest multi process / multi thread execution test case
- 北漂程序员,月薪20K,一年攒15W,正常吗?
- 指针数组和数组指针
- Smart subway | cloud computing injects wisdom into urban subway transportation
- My opinion on how to effectively telecommute | community essay solicitation
- 1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
- 02 specific implementation of LS command
- Unity 绘制弹球和台球的运动轨迹
猜你喜欢

02 specific implementation of LS command

leetcode刷题:二叉树05(翻转二叉树)

Parameterization of controls in katalon

Perf simple process for multithreaded profile

Flink learning 8: data consistency

Unity 绘制弹球和台球的运动轨迹

'2'&gt;' 10'==true? How does JS perform implicit type conversion?

Select sorting and bubble sorting template

I Build a simple microservice project

量子力学习题
随机推荐
RHCSA 08 - automount配置
Redis cluster view the slots of each node
Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
[webrtc] M98 Ninja build and compile instructions
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
dried food! Generation of rare samples based on GaN
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
【罗技】m720
02 specific implementation of LS command
Rhcsa-- day one
vue多级路由嵌套怎么动态缓存组件
Activiti7 task service - process variables (setvariable and setvariablelocal)
【读书会第十三期】多媒体处理工具 FFmpeg 工具集
【CSRF-01】跨站请求伪造漏洞基础原理及攻防
疫情来袭--远程办公之思考|社区征文
ctf-pikachu-XSS
(pointeur) Écrivez - vous une fonction qui compare la taille de la chaîne et fonctionne comme strcmp.
Confession code collection, who says program apes don't understand romance
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
函数计算异步任务能力介绍 - 任务触发去重