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.
1using System.Collections.Generic;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
public class Solution {
public int KthLargestLevelSum(TreeNode root, int k) {
List<int> levelSums = new List<int>();
Dfs(root, 0, levelSums);
if (levelSums.Count < k) return -1;
levelSums.Sort((a, b) => b.CompareTo(a));
return levelSums[k - 1];
}
private void Dfs(TreeNode node, int level, List<int> levelSums) {
if (node == null) return;
if (level == levelSums.Count) levelSums.Add(0);
levelSums[level] += node.val;
Dfs(node.left, level + 1, levelSums);
Dfs(node.right, level + 1, levelSums);
}
}
C# implementation involves using DFS to compute sums at each tree level and sorting these sums to acquire the k-th highest sum result.