111. Minimum Depth of Binary Tree
Easy ↗ Open on LeetCode Source: Misc
Quick review
- Pattern
- Trees
- Time
- O(N)
- Space
- O(H)
Standard Solution (C++)
SummaryGiven the root of a binary tree, find the minimum number of edges from the root to any leaf node (a node with no children), returning 0 for an empty tree.
Approach
- Use BFS (level-order traversal) so the very first leaf encountered is guaranteed to be at the shallowest depth.
- Track the current depth alongside each node as it enters the queue.
- The moment we dequeue a node that has no left or right child, that depth is the answer — return it immediately without scanning the rest of the tree.
- This early-exit property makes BFS preferable to DFS for minimum depth: DFS must traverse all paths to compare, whereas BFS naturally stops at the first leaf.
#include <queue>
using namespace std;
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;
// BFS: each entry is (node, depth). The first leaf we pop is
// at the minimum depth — return immediately.
queue<pair<TreeNode*, int>> q;
q.push({root, 1});
while (!q.empty()) {
auto [node, depth] = q.front();
q.pop();
// A leaf has no children — this is the shallowest such node.
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; // unreachable for non-empty input
}
};
Time O(n) — each node is enqueued and dequeued at most once, though in the best case (left-skewed or balanced) we exit well before visiting all nodes. Space O(w) where w is the maximum width of the tree (at most n/2 for a complete tree), since BFS holds at most one full level in the queue at a time. In the worst case (complete binary tree) this is O(n).
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
#include <queue>
using namespace std;
// 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;
}
std::queue<pair<TreeNode*, int>> q;
q.push({root, 1});
while(!q.empty()){
auto [node, depth]=q.front();
q.pop();
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;
}
};
// Time Complexity: O(N)
// Space Complexity: O(H) where H is the height of the tree. In the worst case (skewed tree), it can be O(N). Time O(N) Space O(H)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.