当前位置:网站首页>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;
}
}

边栏推荐
猜你喜欢

Flink学习8:数据的一致性

Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool

96% of the collected traffic is prevented by bubble mart of cloud hosting

idea修改主体颜色

ctf-pikachu-XSS

Perf simple process for multithreaded profile

*. No main manifest attribute in jar

2020 Bioinformatics | TransformerCPI

dried food! Generation of rare samples based on GaN

Understand the principle of bytecode enhancement technology through the jvm-sandbox source code
随机推荐
Redis cluster uses Lua script. Lua script can also be used for different slots
C语言单向链表练习
Illustrated network: what is the hot backup router protocol HSRP?
Brief explanation of depth first search (with basic questions)
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
Katalon中控件的参数化
【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Two commonly used graphics can easily realize data display
Parameterization of controls in katalon
【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(上)
Flink学习6:编程模型
RHCSA 01 - 创建分区与文件系统
Evolution of MySQL database architecture
ROS2中CMake编译选项的设置
Why is the probability of pod increasing after IPtable
Katalon uses script to query list size
Introduction to asynchronous task capability of function calculation - task trigger de duplication
三年进账35.31亿,这个江西老表要IPO了
leetcode刷题:二叉树08(N叉树的最大深度)