当前位置:网站首页>Binary tree -- 111. Minimum depth of binary tree
Binary tree -- 111. Minimum depth of binary tree
2022-07-26 00:04:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Given a binary tree , Find out the minimum depth .
The minimum depth is the number of nodes on the shortest path from the root node to the nearest leaf node .
explain : A leaf node is a node that has no children .
2 Title Example

Example 2:
Input :root = [2,null,3,null,4,null,5,null,6]
Output :5
3 Topic tips
The number of nodes in the tree ranges from [0, 105] Inside
-1000 <= Node.val <= 1000
4 Ideas
Method 1 : Depth-first search
First of all, you can think of the method of using depth first search , Traverse the whole tree , Record the minimum depth .
For every non leaf node , We only need to calculate the minimum leaf node depth of its left and right subtrees . In this way, a big problem is transformed into a small problem , This problem can be solved recursively .
Complexity analysis
Time complexity :O(N), among N Is the number of nodes in the tree . Access once for each node .
· Spatial complexity :O(H), among H Is the height of the tree . The space complexity mainly depends on the overhead of stack space during recursion , In the worst case , The trees are in chains , The space complexity is O(N). On average, the height of the tree is positively correlated with the logarithm of the number of nodes , The space complexity is O(log N).
Method 2 : Breadth first search
Again , We can think of using breadth first search , Traverse the whole tree . When we find a leaf node , Directly return the depth of this leaf node . The nature of breadth first search ensures the depth of the leaf node first searched — Fixed minimum .
Complexity analysis
Time complexity :O(N), among N Is the number of nodes in the tree . Access once for each node .
· Spatial complexity :O(N), among N Is the number of nodes in the tree . The space complexity mainly depends on the overhead of the queue , The number of elements in the queue will not exceed the number of nodes in the tree .
5 My answer
Method 1 : Depth-first search
class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
int min_depth = Integer.MAX_VALUE;
if (root.left != null) {
min_depth = Math.min(minDepth(root.left), min_depth);
}
if (root.right != null) {
min_depth = Math.min(minDepth(root.right), min_depth);
}
return min_depth + 1;
}
}
Method 2 : Breadth first search
class Solution {
class QueueNode {
TreeNode node;
int depth;
public QueueNode(TreeNode node, int depth) {
this.node = node;
this.depth = depth;
}
}
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<QueueNode> queue = new LinkedList<QueueNode>();
queue.offer(new QueueNode(root, 1));
while (!queue.isEmpty()) {
QueueNode nodeDepth = queue.poll();
TreeNode node = nodeDepth.node;
int depth = nodeDepth.depth;
if (node.left == null && node.right == null) {
return depth;
}
if (node.left != null) {
queue.offer(new QueueNode(node.left, depth + 1));
}
if (node.right != null) {
queue.offer(new QueueNode(node.right, depth + 1));
}
}
return 0;
}
}
边栏推荐
- SIGIR‘22 推荐系统论文之图网络篇
- STM32 serial port
- 如何用yolov5 做个闯红灯监控的智能交通系统(1)
- 【一库】mapbox-gl!一款开箱即用的地图引擎
- ShardingSphere数据分片
- @The underlying principle of Autowired annotation
- Android solves the risk of database injection vulnerability
- Kubernetes网络插件详解 - Calico篇 - 概述
- Bubble sort idea and Implementation
- 痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
猜你喜欢

痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
![牛客/洛谷——[NOIP2003 普及组]栈](/img/95/871b1c6f492b467bffd25912304b44.gif)
牛客/洛谷——[NOIP2003 普及组]栈

安全文档归档软件
![[learning notes] solid works operation record](/img/f7/0535b473755643ce32996f00fa5554.png)
[learning notes] solid works operation record

Observer model of behavioral model

回溯——17. 电话号码的字母组合

YoloV4-tiny网络结构
![[day.2] Joseph Ring problem, how to use arrays to replace circular linked lists (detailed explanation)](/img/2b/b354e52a9eb1d53475fa8d0339d33b.jpg)
[day.2] Joseph Ring problem, how to use arrays to replace circular linked lists (detailed explanation)

二叉树——111. 二叉树的最小深度

NVIDIA cudnn learning
随机推荐
统计之歌 歌词
ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
Android solves the risk of database injection vulnerability
Annotation @autowired source code analysis
模块二作业
Observer model of behavioral model
Song of statistics lyrics
Interview focus - TCP protocol of transport layer
C language implementation of three chess
The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
SQLZOO——Nobel Quiz
开放API生态系统面临的十个威胁
Storage of data in memory
Detailed explanation of kubernetes network plug-ins - calico chapter - Overview
[one library] mapbox GL! A map engine out of the box
Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
The GUI interface of yolov3 (simple, image detection)
2022-07-18 study notes of group 5 self-cultivation class (every day)
本轮牛市还能持续多久?|疑问解答 2021-05-11
[learning notes] unreal 4 engine introduction (IV)