当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

Key and difficult points of C language pointer

The GUI interface of yolov3 (simple, image detection)

Part 66: monocular 3D reconstruction point cloud

Lua script Wireshark plug-in to resolve third-party private protocols

Solve the problem of rapid index bar extrusion

NVIDIA cuDNN学习

NVIDIA可编程推理加速器TensorRT学习笔记(三)——加速推理

SQLZOO——Nobel Quiz

Leetcode200-查找岛屿数量详解

如何用yolov5 做个闯红灯监控的智能交通系统(1)
随机推荐
NVIDIA cudnn learning
1223. Dice simulation range DP
SIGIR '22 recommendation system paper graph network
二叉树——101. 对称二叉树
Bubble sort idea and Implementation
模式之固定与交替顺序执行
Compile live555 with vs2019 in win10
栈与队列——347. 前 K 个高频元素
C# - readonly 和 const 关键字
Basic syntax of MySQL DDL, DML and DQL
二叉树——110. 平衡二叉树
Ten threats to open API ecosystem
Exercise (3) create a list set (both ArrayList and LinkedList)
JS synchronization and asynchrony
QT smart pointer error prone point
Observer model of behavioral model
Part 74: overview of machine learning optimization methods and superparameter settings
Cherish time and improve efficiency
C language actual combat guessing game
【英雄哥七月集训】第 24天: 线性树