0% completed
Problem Statement
Given a root of the binary tree, find the minimum depth of a binary tree.
The minimum depth is the number of nodes along the shortest path from the root node to the nearest leaf node.
Examples
Example 1
Example 2
Constraints:
- The number of nodes in the tree is in the range [0, 10<sup>5</sup>].
-1000 <= Node.val <= 1000
Solution
To solve this problem, we use a Breadth-First Search (BFS) approach to find the minimum depth of a binary tree. BFS is ideal for this problem because it explores nodes level by level, ensuring that we find the closest leaf node (node without children) to the root as quickly as possible.
Starting from the root, we traverse the tree level by level, and for each node, we check if it is a leaf node. The moment we encounter the first leaf node, we return the depth of that node as it represents the minimum depth of the tree. By using a queue to keep track of nodes at each level, we can efficiently manage this traversal.
Step-by-Step Algorithm
-
Check if the tree is empty:
- If the
rootisnull, return0as the tree has no depth.
- If the
-
Initialize the BFS queue:
- Create an empty queue and add the
rootnode to it. - Initialize a variable
minimumTreeDepthto0to keep track of the depth as we traverse each level.
- Create an empty queue and add the
-
Start BFS traversal:
- While the queue is not empty, perform the following steps:
- Increment the
minimumTreeDepthby1to account for the current level. - Determine the number of nodes at the current level by checking the size of the queue.
- Increment the
- While the queue is not empty, perform the following steps:
-
Process each node at the current level:
- For each node in the current level, perform the following:
- Dequeue the node from the queue.
- Check if the node is a leaf node (both left and right children are
null). - If it is a leaf node, return the current
minimumTreeDepthas this is the minimum depth of the tree.
- For each node in the current level, perform the following:
-
Add children to the queue:
- If the node is not a leaf, add its left and right children to the queue (if they exist) to be processed in the next level.
-
Repeat until the queue is empty:
- Continue processing each level until the queue is empty, which indicates that all nodes have been traversed. The first leaf node encountered will provide the minimum depth.
Algorithm Walkthrough
-
Initialization:
- Start with the root node
12. - Initialize an empty queue and add the root node
12to it. - Set
minimumTreeDepthto0.
- Start with the root node
-
Start BFS Traversal:
- The queue contains
[12]. - Increment
minimumTreeDepthto1because we are starting at the first level.
- The queue contains
-
Process the first level (root node
12):- The size of the queue is
1, indicating one node at this level. - Dequeue node
12from the queue. - Check if node
12is a leaf node (both left and right children arenull):- Node
12is not a leaf node, as it has left and right children.
- Node
- Add the left child
7and the right child1of node12to the queue. - The queue now contains
[7, 1].
- The size of the queue is
-
Process the second level (nodes
7and1):-
The queue contains
[7, 1]. -
Increment
minimumTreeDepthto2because we are moving to the second level. -
The size of the queue is
2, indicating two nodes at this level. -
Processing node
7:- Dequeue node
7from the queue. - Check if node
7is a leaf node:- Node
7is a leaf node (no left or right children).
- Node
- Since node
7is a leaf node, return the currentminimumTreeDepth, which is2.
- Dequeue node
-
Final Output: The minimum depth of the tree [12, 7, 1, null, null, 10, 5] is 2.
Code
Here is the code for this algorithm:
Complexity Analysis
Time Complexity
The time complexity of the above algorithm is O(N), where ‘N’ is the total number of nodes in the tree. This is due to the fact that we traverse each node once.
Space Complexity
The space complexity of the above algorithm will be O(N) which is required for the queue. Since we can have a maximum of N/2 nodes at any level (this could happen only at the lowest level), therefore we will need O(N) space to store them in the queue.
Similar Problems
Problem 1: Given a binary tree, find its maximum depth (or height) using Tree BFS traversal.
Solution: We will follow a similar approach. Instead of returning as soon as we find a leaf node, we will keep traversing for all the levels, incrementing maximumDepth each time we complete a level. Here is what the code will look like:
Here is the visual representation of the algorithm:
Code
Here is the code for this algorithm:
Complexity Analysis
Time Complexity
The time complexity of the above algorithm is O(N), where ‘N’ is the total number of nodes in the tree. This is due to the fact that we traverse each node once.
Space Complexity
The space complexity of the above algorithm will be O(N) which is required for the queue. Since we can have a maximum of N/2 nodes at any level (this could happen only at the lowest level), therefore we will need O(N) space to store them in the queue.
On this page
Problem Statement
Examples
Solution
Step-by-Step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity
Space Complexity
Similar Problems
Code
Complexity Analysis
Time Complexity
Space Complexity