Sponsored
Sponsored
In this approach, we utilize Breadth-First Search (BFS) to traverse the binary tree level by level. As we traverse each level, we compute the sum of the node values at that level. We store these sums in a min-heap of size 'k'. If the heap grows beyond size 'k', we remove the smallest element, ensuring that at the end, the heap contains the 'k' largest level sums. This allows us to efficiently retrieve the kth largest level sum by looking at the top of the min-heap.
Time Complexity: O(n log k), where 'n' is the number of nodes in the tree. We traverse all nodes once, and each heap operation takes O(log k).
Space Complexity: O(max(w, k)), where 'w' is the maximum width of the tree at any level (for the queue), and 'k' is the size of the heap.
1import java.util.*;
2
3class TreeNode {
4 int val;
5 TreeNode left;
6 TreeNode right;
7 TreeNode(int val) { this.val = val; }
8}
9
10public class Solution {
11 public int kthLargestLevelSum(TreeNode root, int k) {
12 if (root == null) return -1;
13 Queue<TreeNode> queue = new LinkedList<>();
14 PriorityQueue<Integer> minHeap = new PriorityQueue<>();
15 queue.offer(root);
16
17 while (!queue.isEmpty()) {
18 int levelSize = queue.size();
19 int levelSum = 0;
20 for (int i = 0; i < levelSize; i++) {
21 TreeNode node = queue.poll();
22 levelSum += node.val;
23 if (node.left != null) queue.offer(node.left);
24 if (node.right != null) queue.offer(node.right);
25 }
26 minHeap.offer(levelSum);
27 if (minHeap.size() > k) {
28 minHeap.poll();
29 }
30 }
31 return minHeap.size() == k ? minHeap.peek() : -1;
32 }
33}
The Java solution involves BFS to capture level sums and utilizes a PriorityQueue as a min-heap to find the 'k' largest sums efficiently.
This approach utilizes a depth-first search (DFS) technique with a modification that tracks and accumulates the sum values per level in an array or list. Once all levels are processed with DFS, we sort the resulting list of sums. If the list has fewer than 'k' entries, we return -1; otherwise, we return the k-th largest value by accessing the list.
Time Complexity: O(n + d log d), where 'n' is the number of nodes and 'd' is the depth of the tree (to sort the level sums).
Space Complexity: O(d), representing the recursion stack and array for level sums, where 'd' is tree depth.
1#include <algorithm>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void dfs(TreeNode* node, int level, vector<int>& levelSums) {
if (!node) return;
if (level == levelSums.size()) levelSums.push_back(0);
levelSums[level] += node->val;
dfs(node->left, level + 1, levelSums);
dfs(node->right, level + 1, levelSums);
}
int kthLargestLevelSum(TreeNode* root, int k) {
vector<int> levelSums;
dfs(root, 0, levelSums);
if (levelSums.size() < k) return -1;
sort(levelSums.begin(), levelSums.end(), greater<int>());
return levelSums[k - 1];
}
The C++ version tackles depth-based accumulation using DFS, after which it sorts the levels' sums and determines the k-th largest sum by selecting from the sorted results.