Problem Description
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.
Examples
Example 1: Input: root = [3,9,20,null,null,15,7] Output: 2 Example 2: Input: root = [2,null,3,null,4,null,5,null,6] Output: 5
Python Solution
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from collections import deque
def minDepth(root: Optional[TreeNode]) -> int:
if not root:
return 0
# Use BFS for shortest path
queue = deque([(root, 1)])
while queue:
node, depth = queue.popleft()
# If we find a leaf node, this is the minimum depth
if not node.left and not node.right:
return depth
if node.left:
queue.append((node.left, depth + 1))
if node.right:
queue.append((node.right, depth + 1))
return 0 # Should never reach here for valid binary tree
Java Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
// Use BFS with queue of node-depth pairs
Queue> queue = new LinkedList<>();
queue.offer(new Pair<>(root, 1));
while (!queue.isEmpty()) {
Pair pair = queue.poll();
TreeNode node = pair.getKey();
int depth = pair.getValue();
// If we find a leaf node, this is the minimum depth
if (node.left == null && node.right == null) {
return depth;
}
if (node.left != null) {
queue.offer(new Pair<>(node.left, depth + 1));
}
if (node.right != null) {
queue.offer(new Pair<>(node.right, depth + 1));
}
}
return 0; // Should never reach here for valid binary tree
}
}
C++ Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) {
return 0;
}
// Use BFS with queue of node-depth pairs
queue> q;
q.push({root, 1});
while (!q.empty()) {
auto [node, depth] = q.front();
q.pop();
// If we find a leaf node, this is the minimum depth
if (!node->left && !node->right) {
return depth;
}
if (node->left) {
q.push({node->left, depth + 1});
}
if (node->right) {
q.push({node->right, depth + 1});
}
}
return 0; // Should never reach here for valid binary tree
}
};
JavaScript Solution
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var minDepth = function(root) {
if (!root) {
return 0;
}
// Use BFS with queue of node-depth pairs
const queue = [[root, 1]];
while (queue.length) {
const [node, depth] = queue.shift();
// If we find a leaf node, this is the minimum depth
if (!node.left && !node.right) {
return depth;
}
if (node.left) {
queue.push([node.left, depth + 1]);
}
if (node.right) {
queue.push([node.right, depth + 1]);
}
}
return 0; // Should never reach here for valid binary tree
};
C# Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int MinDepth(TreeNode root) {
if (root == null) {
return 0;
}
// Use BFS with queue of node-depth pairs
var queue = new Queue<(TreeNode node, int depth)>();
queue.Enqueue((root, 1));
while (queue.Count > 0) {
var (node, depth) = queue.Dequeue();
// If we find a leaf node, this is the minimum depth
if (node.left == null && node.right == null) {
return depth;
}
if (node.left != null) {
queue.Enqueue((node.left, depth + 1));
}
if (node.right != null) {
queue.Enqueue((node.right, depth + 1));
}
}
return 0; // Should never reach here for valid binary tree
}
}
Complexity Analysis
- Time Complexity: O(n) where n is the number of nodes in the tree
- Space Complexity: O(w) where w is the maximum width of the tree
Solution Explanation
This solution uses Breadth-First Search (BFS) to find the minimum depth:
- Key concept:
- BFS guarantees shortest path
- Track depth with nodes
- Early termination
- Algorithm steps:
- Use queue for BFS
- Track node depth
- Check for leaf nodes
- Return first leaf depth
Key points:
- Handle empty tree
- Efficient traversal
- Early termination
- Level-wise search