Watch 10 video solutions for Kth Largest Sum in a Binary Tree, a medium level problem involving Tree, Breadth-First Search, Sorting. This walkthrough by NeetCodeIO has 7,973 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given the root of a binary tree and a positive integer k.
The level sum in the tree is the sum of the values of the nodes that are on the same level.
Return the kth largest level sum in the tree (not necessarily distinct). If there are fewer than k levels in the tree, return -1.
Note that two nodes are on the same level if they have the same distance from the root.
Example 1:
Input: root = [5,8,9,2,1,3,7,4,6], k = 2 Output: 13 Explanation: The level sums are the following: - Level 1: 5. - Level 2: 8 + 9 = 17. - Level 3: 2 + 1 + 3 + 7 = 13. - Level 4: 4 + 6 = 10. The 2nd largest level sum is 13.
Example 2:
Input: root = [1,2,null,3], k = 1 Output: 3 Explanation: The largest level sum is 3.
Constraints:
n.2 <= n <= 1051 <= Node.val <= 1061 <= k <= nProblem Overview: You are given the root of a binary tree. Each level of the tree has a sum formed by adding the values of all nodes on that level. The task is to compute every level sum and return the kth largest among them. If the tree has fewer than k levels, return -1.
Approach 1: Breadth-First Search with Min-Heap (O(n log k) time, O(w + k) space)
This approach processes the tree level by level using Breadth-First Search. Use a queue to iterate through each level and compute the sum of node values at that level. Instead of storing every level sum, maintain a min-heap of size k. After computing a level sum, push it into the heap; if the heap size exceeds k, remove the smallest element. The heap always stores the k largest level sums seen so far, and the root of the heap is the kth largest. This reduces sorting overhead and avoids storing all sums when the tree has many levels.
The time complexity is O(n log k) because every node is visited once and each heap operation costs log k. Space complexity is O(w + k), where w is the maximum tree width due to the BFS queue. This approach is typically the most efficient when k is much smaller than the number of levels.
Approach 2: Depth-First Search with Level Tracking (O(n + L log L) time, O(L) space)
This approach uses tree traversal with DFS while tracking the current depth. Maintain an array (or hash map) where the index represents the level and the value stores the cumulative sum of nodes at that level. During recursion, add the node value to the corresponding level entry. If the level does not exist yet, append a new entry.
After the traversal completes, the array contains all level sums. Sort the array in descending order using a standard sorting algorithm and return the element at index k - 1. If the array length is smaller than k, return -1. DFS visits each node exactly once, giving O(n) traversal time. Sorting the L level sums adds O(L log L), where L is the number of levels.
This method is straightforward and easy to implement. It works well when the number of levels is relatively small and when you prefer recursive traversal over queue-based iteration.
Recommended for interviews: BFS with a min-heap is usually the expected optimal solution. It demonstrates strong understanding of binary tree traversal and heap optimization. A brute-force style solution that collects all sums and sorts them shows basic problem understanding, but maintaining a heap of size k proves you can reduce unnecessary work and control memory usage.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| BFS with Min-Heap | O(n log k) | O(w + k) | Best general solution when k is small relative to number of levels |
| DFS with Level Tracking + Sort | O(n + L log L) | O(L) | Simpler implementation when storing all level sums is acceptable |
| BFS + Collect and Sort | O(n + L log L) | O(L) | Straightforward approach when optimization is not required |