Watch 10 video solutions for Maximum Level Sum of a Binary Tree, a medium level problem involving Tree, Depth-First Search, Breadth-First Search. This walkthrough by codestorywithMIK has 11,116 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
Return the smallest level x such that the sum of all the values of nodes at level x is maximal.
Example 1:
Input: root = [1,7,0,7,-8,null,null] Output: 2 Explanation: Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + -8 = -1. So we return the level with the maximum sum which is level 2.
Example 2:
Input: root = [989,null,10250,98693,-89388,null,null,null,-32127] Output: 2
Constraints:
[1, 104].-105 <= Node.val <= 105Problem Overview: You’re given the root of a binary tree. Each level contains several nodes, and each node has an integer value. The task is to compute the sum of values at every level and return the level index (1-indexed) that has the maximum sum.
The key observation: nodes on the same depth belong to the same level. If you process the tree level by level, you can compute the sum for each level and track the maximum as you go.
Approach 1: Breadth-First Search (Level Order Traversal) (Time: O(n), Space: O(w))
This approach uses Breadth-First Search to traverse the binary tree level by level. Start by pushing the root node into a queue. For each iteration, process all nodes currently in the queue (which represent one level). Sum their values, enqueue their left and right children, and compare the level sum with the current maximum.
The queue naturally separates nodes by level because children are added only after their parent level is processed. Maintain variables for currentLevel, maxSum, and resultLevel. Whenever a level sum exceeds maxSum, update the answer. This approach is straightforward and matches how most interviewers expect you to handle level-based tree problems.
Time complexity is O(n) since each node is processed exactly once. Space complexity is O(w), where w is the maximum width of the tree, due to the queue storing nodes of the current level.
Approach 2: Depth-First Search with Level Tracking (Time: O(n), Space: O(h))
A recursive solution using Depth-First Search also works. Instead of processing by levels explicitly, DFS tracks the current depth while traversing the tree. Maintain a dynamic array where index i stores the cumulative sum for level i. During recursion, add the node’s value to the corresponding level entry.
If the DFS reaches a depth that hasn’t been seen before, append a new element to the array. After the traversal completes, scan the array to find the level with the maximum sum. This approach separates traversal from result evaluation and works well in recursive implementations.
Time complexity remains O(n) because every node is visited once. Space complexity is O(h) for recursion stack depth (where h is tree height) plus O(L) for storing level sums.
Recommended for interviews: The BFS level-order traversal is typically the expected solution. The problem explicitly revolves around levels, so using a queue to process nodes level by level demonstrates a clear understanding of tree traversal patterns. The DFS approach is still valid and shows flexibility, but BFS is usually the first method interviewers want to see.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Breadth-First Search (Level Order) | O(n) | O(w) | Best for problems involving level-by-level processing of a binary tree |
| Depth-First Search with Level Tracking | O(n) | O(h) + O(L) | Useful when implementing recursive traversal or when tracking level data during DFS |