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

边栏推荐
- [PaddleSeg 源码阅读] PaddleSeg计算 mIoU
- LevelDB源码解读-SkipList
- Detailed explanation of PPTC self recovery fuse
- Introduction to asynchronous task capability of function calculation - task trigger de duplication
- Epidemic strikes -- Thinking about telecommuting | community essay solicitation
- postgresql 用户不能自己创建表格配置
- Redis cluster view the slots of each node
- Storage of MySQL database
- [untitled]
- [Yugong series] go teaching course 002 go language environment installation in July 2022
猜你喜欢

SQL statement strengthening exercise (MySQL 8.0 as an example)

1289_ Implementation analysis of vtask suspend() interface in FreeRTOS

三年进账35.31亿,这个江西老表要IPO了

Sales management system of lightweight enterprises based on PHP

Idea modify body color

Flink学习8:数据的一致性

MySQL one master multiple slaves + linear replication

Evolution of MySQL database architecture

Flink学习6:编程模型

毕业设计:设计秒杀电商系统
随机推荐
vue多级路由嵌套怎么动态缓存组件
STM32外接DHT11显示温湿度
Global exposure and roller shutter exposure of industrial cameras
Support the first triggered go ticker
SQL statement strengthening exercise (MySQL 8.0 as an example)
[paddleseg source code reading] normalize operation of paddleseg transform
Simple dialogue system -- text classification using transformer
Introduction to asynchronous task capability of function calculation - task trigger de duplication
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
Select sorting and bubble sorting template
【webrtc】m98 ninja 构建和编译指令
There is a problem that the package cannot be parsed in the like project
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
Katalon使用script实现查询List大小
Objective-C member variable permissions
Interpretation of leveldb source code skiplist
Spa in SDP
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
Tcpclientdemo for TCP protocol interaction
Flink学习8:数据的一致性