Skip to content

Commit e0d3fca

Browse files
committed
Time: 40 ms (27.26%), Space: 62.4 MB (80.65%) - LeetHub
1 parent 1afd1c7 commit e0d3fca

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public long kthLargestLevelSum(TreeNode root, int k) {
18+
List<Long> list=new ArrayList<>();
19+
Queue<TreeNode> qu=new LinkedList<>();
20+
qu.offer(root);
21+
while(!qu.isEmpty())
22+
{
23+
int p=qu.size();
24+
long s=0;
25+
for(int i=0;i<p;i++)
26+
{
27+
if(qu.peek().left!=null)
28+
qu.offer(qu.peek().left);
29+
if(qu.peek().right!=null)
30+
qu.offer(qu.peek().right);
31+
s+=(long)qu.poll().val;
32+
}
33+
list.add(s);
34+
}
35+
Collections.sort(list);
36+
return k > list.size() ? -1 : list.get(list.size()-k);
37+
}
38+
}

0 commit comments

Comments
 (0)