Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example 1:
Consider the following binary tree:
3
/ \
9 20
/ \
15 7
What is the minimum depth of this tree? The answer is 2, as the shortest path from the root (3) to a leaf node (9) is of length 2.
Example 2:
Consider the following binary tree:
2
\
3
\
4
\
5
\
6
What is the minimum depth of this tree? The answer is 5, as the shortest path from the root (2) to a leaf node (6) is of length 5.
Constraints:
[0, 10^5]
.-1000 <= Node.val <= 1000
Write a function that takes the root of a binary tree as input and returns its minimum depth. Discuss the time and space complexity of your solution. Consider edge cases such as an empty tree or a tree with only one node.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to find the minimum depth of a tree is like exploring every possible path from the top (root) down to the bottom (leaves). We check each path individually to see how far it goes. Ultimately, we compare all path lengths to find the shortest one.
Here's how the algorithm would work step-by-step:
def minimum_depth_of_binary_tree_brute_force(root):
if not root:
return 0
all_depths = []
def calculate_depth(node, current_depth):
# If we've reached a leaf node,
# record the depth.
if not node.left and not node.right:
all_depths.append(current_depth)
return
# Recursively explore the left subtree
# if it exists.
if node.left:
calculate_depth(node.left, current_depth + 1)
# Recursively explore the right subtree
# if it exists.
if node.right:
calculate_depth(node.right, current_depth + 1)
# Start the depth calculation from the root node
# with an initial depth of 1.
calculate_depth(root, 1)
# Return the minimum depth from the list of all depths
# after checking all paths
return min(all_depths)
We want to find the shortest path from the top of the tree to the nearest 'leaf' (a node with no children). The best way to do this is to explore the tree level by level, stopping as soon as we find a leaf.
Here's how the algorithm would work step-by-step:
from collections import deque
def minimum_depth_of_binary_tree(root):
if not root:
return 0
queue = deque([(root, 1)])
while queue:
node, current_depth = queue.popleft()
# Check if current node is a leaf
if not node.left and not node.right:
return current_depth
# If there's a left child, add it to the queue
if node.left:
queue.append((node.left, current_depth + 1))
# If there's a right child, add it to the queue
if node.right:
queue.append((node.right, current_depth + 1))
class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
Case | How to Handle |
---|---|
Null or empty root node | Return 0 if the root is null, as an empty tree has a minimum depth of 0. |
Tree with only a root node (no children) | Return 1 if the root has no children, as it represents a path of length 1. |
Highly unbalanced tree (skewed left or right) | The algorithm should correctly traverse the longer path, correctly identifying when the shorter leaf is found and returning that minimum depth. |
Tree with only left child nodes | The algorithm traverses to the left-most leaf and returns the depth. |
Tree with only right child nodes | The algorithm traverses to the right-most leaf and returns the depth. |
Large tree depth (potential stack overflow with recursive approaches) | Consider using iterative BFS (Breadth-First Search) to avoid exceeding recursion depth limits and memory constraints. |
Tree with a mix of null and non-null children nodes at different levels | Ensure the algorithm correctly identifies leaf nodes (nodes with no children) and doesn't incorrectly terminate at a node with a null child. |
Integer overflow for very deep trees if depth is stored as an integer | While highly unlikely, consider alternative data types with larger capacity if handling extremely deep trees is a requirement. |