当前位置:网站首页>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;
}
}
边栏推荐
- 【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
- Two commonly used graphics can easily realize data display
- Katalon使用script实现查询List大小
- [Logitech] m720
- User defined path and file name of Baidu editor in laravel admin
- MySQL maxscale realizes read-write separation
- Evolution of MySQL database architecture
- Objective-C member variable permissions
- Tcpclientdemo for TCP protocol interaction
- Katalon framework tests web (XXI) to obtain element attribute assertions
猜你喜欢
Go 语言入门很简单:Go 实现凯撒密码
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
laravel admin里百度编辑器自定义路径和文件名
02 specific implementation of LS command
Sales management system of lightweight enterprises based on PHP
10 reasons for not choosing to use free virtual hosts
ctf-pikachu-XSS
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
图解网络:什么是热备份路由器协议HSRP?
Flink学习6:编程模型
随机推荐
函数计算异步任务能力介绍 - 任务触发去重
Katalon framework tests web (XXI) to obtain element attribute assertions
idea修改主体颜色
新型数据中心,助力加快构建以数据为关键要素的数字经济
[PaddleSeg 源码阅读] PaddleSeg Transform 的 Normalize操作
MySQL maxscale realizes read-write separation
Introduction to asynchronous task capability of function calculation - task trigger de duplication
The maximum expiration time of client secret in azure ad application registration is modified to 2 years
Storage of MySQL database
Deep thinking on investment
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
量子力学习题
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
STM32 external DHT11 display temperature and humidity
[Yugong series] go teaching course 002 go language environment installation in July 2022
【CSRF-01】跨站请求伪造漏洞基础原理及攻防
JDBC advanced
The new data center helps speed up the construction of a digital economy with data as a key element
Tcpclientdemo for TCP protocol interaction
Class summation, shortest row