当前位置:网站首页>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了
- functools下的reduce函数
- 2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
- 用于TCP协议交互的TCPClientDemo
- MySQL one master multiple slaves + linear replication
- VIM mapping command
- [PaddleSeg 源码阅读] PaddleSeg计算 mIoU
- 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
- Rhcsa-- day one
- JDBC advanced
猜你喜欢
My opinion on how to effectively telecommute | community essay solicitation
如何有效远程办公之我见 | 社区征文
Introduction to asynchronous task capability of function calculation - task trigger de duplication
ctf-pikachu-XSS
postgresql 用户不能自己创建表格配置
量子力学习题
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
分布式系统:what、why、how
[untitled]
Objective-C description method and type method
随机推荐
量子力学习题
渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
Calculate the odd sum of 1~n (1~100 as an example)
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?
Objective-C member variable permissions
01 qemu 启动编译好的镜像 VFS: Unable to mount root fs on unknown-block(0,0)
vue多级路由嵌套怎么动态缓存组件
ctf-pikachu-XSS
Support the first triggered go ticker
[PaddleSeg 源码阅读] PaddleSeg 自定义数据类
MySQL maxscale realizes read-write separation
The new data center helps speed up the construction of a digital economy with data as a key element
"Implement both software and hardware" to help build a new cloud computing data center
Rhcsa-- day one
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
mysql数据库的存储
Object oriented -- encapsulation, inheritance, polymorphism
数据库SQL语句汇总,持续更新......
Simple dialogue system -- text classification using transformer
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量