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

边栏推荐
- 三年进账35.31亿,这个江西老表要IPO了
- 渗透实战-SQLServer提权
- Objective C attribute keyword
- I Build a simple microservice project
- [paddleseg source code reading] paddleseg custom data class
- SQL语句加强练习(MySQL8.0为例)
- Reduce function under functools
- idea修改主体颜色
- EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?
- [paddleseg source code reading] paddleseg calculation dice
猜你喜欢

Activiti7 task service - process variables (setvariable and setvariablelocal)

Two commonly used graphics can easily realize data display

SQL語句加强練習(MySQL8.0為例)

My opinion on how to effectively telecommute | community essay solicitation

EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?

The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
![CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构](/img/ba/c1d40de154344ccc9f2fd1dd4cb12f.png)
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。

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

Illustrated network: what is the hot backup router protocol HSRP?
随机推荐
Database SQL statement summary, continuous update
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
pytest多进程/多线程执行测试用例
Global exposure and roller shutter exposure of industrial cameras
智慧地铁| 云计算为城市地铁交通注入智慧
The maximum expiration time of client secret in azure ad application registration is modified to 2 years
毕业设计:设计秒杀电商系统
"Implement both software and hardware" to help build a new cloud computing data center
JDBC 进阶
10 reasons for not choosing to use free virtual hosts
Object oriented -- encapsulation, inheritance, polymorphism
[PaddleSeg 源码阅读] PaddleSeg计算Dice
Katalon framework test web (XXVI) automatic email
[untitled]
Two commonly used graphics can easily realize data display
Objective-C member variable permissions
The difference between bagging and boosting in machine learning
*. No main manifest attribute in jar
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
Smart subway | cloud computing injects wisdom into urban subway transportation