Find the Level of Tree with Minimum Sum - Problem
Given the root of a binary tree 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.
Input & Output
Example 1 — Basic Case
$
Input:
root = [1,7,0,7,-8,null,null]
›
Output:
3
💡 Note:
Level 1: sum = 1, Level 2: sum = 7+0 = 7, Level 3: sum = 7+(-8) = -1. Level 3 has the minimum sum of -1.
Example 2 — Single Node
$
Input:
root = [5]
›
Output:
1
💡 Note:
Only one level with sum = 5, so return level 1.
Example 3 — Tie Case
$
Input:
root = [1,2,3,4,5,6,7]
›
Output:
1
💡 Note:
Level 1: sum = 1, Level 2: sum = 2+3 = 5, Level 3: sum = 4+5+6+7 = 22. Level 1 has minimum sum.
Constraints
- The number of nodes in the tree is in the range [1, 104]
- -105 ≤ Node.val ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Tree
Binary tree with node values [1,7,0,7,-8,null,null]
2
Level Sums
Calculate sum for each level: L1=1, L2=7, L3=-1
3
Find Minimum
Level 3 has minimum sum of -1
Key Takeaway
🎯 Key Insight: Use level-order traversal to process complete levels and track minimum sum
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code