forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2583.java
32 lines (29 loc) · 1.03 KB
/
_2583.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.*;
public class _2583 {
public static class Solution1 {
public long kthLargestLevelSum(TreeNode root, int k) {
List<Long> list = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
long thisSum = 0L;
for (int i = 0; i < size; i++) {
TreeNode curr = queue.poll();
thisSum += curr.val;
if (curr.left != null) {
queue.offer(curr.left);
}
if (curr.right != null) {
queue.offer(curr.right);
}
}
list.add(thisSum);
}
Collections.sort(list, Collections.reverseOrder());
return k > list.size() ? -1 : list.get(k - 1);
}
}
}