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.
1using System;
2using System.Collections.Generic;
3
4public class TreeNode {
5 public int val;
6 public TreeNode left;
7 public TreeNode right;
8 public TreeNode(int x) { val = x; }
9}
10
11public class Solution {
12 public int KthLargestLevelSum(TreeNode root, int k) {
13 if (root == null) return -1;
14 Queue<TreeNode> queue = new Queue<TreeNode>();
15 PriorityQueue<int, int> minHeap = new PriorityQueue<int, int>();
16 queue.Enqueue(root);
17
18 while (queue.Count > 0) {
19 int levelSize = queue.Count;
20 int levelSum = 0;
21 for (int i = 0; i < levelSize; i++) {
22 TreeNode node = queue.Dequeue();
23 levelSum += node.val;
24 if (node.left != null) queue.Enqueue(node.left);
25 if (node.right != null) queue.Enqueue(node.right);
26 }
27 minHeap.Enqueue(levelSum, levelSum);
28 if (minHeap.Count > k) {
29 minHeap.Dequeue();
30 }
31 }
32 return minHeap.Count == k ? minHeap.Peek() : -1;
33 }
34}
The C# solution deploys BFS for level sums and uses a priority queue to retain the 'k' largest sums, with standard queue operations.
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.