当前位置:网站首页>leetcode刷题:二叉树09(二叉树的最小深度)
leetcode刷题:二叉树09(二叉树的最小深度)
2022-07-04 03:51:00 【涛涛英语学不进去】
111.二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],

返回它的最小深度 2.
依旧使用层序遍历,当这层不满,且!!!出现了叶子节点,则出现最小深度。
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. 二叉树的最小深度 * https://leetcode.cn/problems/minimum-depth-of-binary-tree/ **/
public class MinDepth {
public int minDepth(TreeNode root) {
//层序遍历,哪一行不满,即有缺省,则为最短深度
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();
//出现叶子结点
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;
}
}

边栏推荐
- Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
- Storage of MySQL database
- Es network layer
- Objective-C description method and type method
- 用于TCP协议交互的TCPClientDemo
- Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
- 2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
- pytest多进程/多线程执行测试用例
- Evolution of MySQL database architecture
- 【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
猜你喜欢

10 reasons for not choosing to use free virtual hosts

Storage of MySQL database

Msgraphmailbag - search only driveitems of file types

Wechat official account web page authorization

MySQL maxscale realizes read-write separation

Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡

Go 语言入门很简单:Go 实现凯撒密码

Idea modify body color

2021 RSC | Drug–target affinity prediction using graph neural network and contact maps

ctf-pikachu-XSS
随机推荐
vim正确加区间注释
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
Object oriented -- encapsulation, inheritance, polymorphism
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
Database SQL statement summary, continuous update
Graduation summary
Katalon中控件的参数化
毕业三年,远程半年 | 社区征文
Class summation, shortest row
Idea modify body color
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
[paddleseg source code reading] normalize operation of paddleseg transform
JS实现文字滚动 跑马灯效果
There is a problem that the package cannot be parsed in the like project
Reduce function under functools
函数计算异步任务能力介绍 - 任务触发去重
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
Spa in SDP
SQL statement strengthening exercise (MySQL 8.0 as an example)