Watch the video solution for Find the Level of Tree with Minimum Sum, a medium level problem involving Tree, Depth-First Search, Breadth-First Search. This walkthrough by Programming Live with Larry has 196 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given the root of a binary tree root where each node has a value, return the level of the tree that has the minimum sum of values among all the levels (in case of a tie, return the lowest level).
Note that the root of the tree is at level 1 and the level of any other node is its distance from the root + 1.
Example 1:
Input: root = [50,6,2,30,80,7]
Output: 2
Explanation:

Example 2:
Input: root = [36,17,10,null,null,24]
Output: 3
Explanation:

Example 3:
Input: root = [5,null,5,null,5]
Output: 1
Explanation:

Constraints:
[1, 105].1 <= Node.val <= 109Problem Overview: Given a binary tree, compute the sum of values at each level and return the level index that has the minimum total. Levels are processed from the root downward, so level 1 contains the root, level 2 contains its children, and so on.
Approach 1: Breadth-First Search / Level Order Traversal (O(n) time, O(w) space)
The most direct solution uses Breadth-First Search to process the tree level by level. Push the root into a queue, then repeatedly remove nodes from the queue while adding their children back. For each iteration, measure the current queue size to determine how many nodes belong to the current level, iterate through them, and compute the level sum.
Track two variables while traversing: the current level number and the smallest sum seen so far. After processing all nodes of a level, compare the computed sum with the minimum sum and update the answer if needed. Because every node is visited exactly once and each edge is processed once when adding children to the queue, the total runtime is O(n). The queue stores at most one level of nodes at a time, which requires O(w) space where w is the maximum tree width.
This approach fits naturally with problems involving level-based calculations in a binary tree. You avoid additional bookkeeping since the traversal order already groups nodes by level.
Approach 2: Depth-First Search with Level Tracking (O(n) time, O(h) space)
A second option uses Depth-First Search to accumulate sums per level. During recursion, pass the current depth and maintain an array or hash map where levelSum[depth] stores the total for that level. Each node contributes its value to the corresponding index before recursively exploring its left and right children.
After the traversal finishes, iterate through the stored level sums to find the smallest value and return its level index. The DFS visits each node once, giving O(n) time complexity. The recursion stack requires O(h) space where h is the tree height, plus the storage for level sums.
DFS is useful when you already perform recursive traversals or when the tree is narrow but deep. However, it requires an extra data structure to group nodes by level.
Recommended for interviews: The BFS level-order traversal is the expected solution. It directly mirrors the problem statement: process the tree one level at a time and compute a running sum. Showing the DFS alternative demonstrates deeper understanding, but the BFS implementation is simpler, avoids extra passes, and clearly communicates how level-based tree problems should be handled.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Breadth-First Search (Level Order) | O(n) | O(w) | Best for level-based calculations and typical interview solutions |
| Depth-First Search with Level Tracking | O(n) | O(h) | Useful when using recursive tree traversal or when storing level aggregates |