2583. Kth Largest Sum in a Binary Tree

problem

solution

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
33
34
35
36
37
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
long long kthLargestLevelSum(TreeNode* root, int k) {
vector<long long> levels;
// level traversal
queue<TreeNode*> q({root});
while(!q.empty()) {
int size = q.size();
long long level = 0;
for(int i=0;i<size; ++i) {
TreeNode *node = q.front();
q.pop();
level+=node->val;
if(node->left != nullptr) q.push(node->left);
if(node->right != nullptr) q.push(node->right);
// cout<<node->val<<endl;
}
levels.push_back(level);
}

sort(levels.begin(), levels.end(), greater<long long>());
if( k-1 >= levels.size()) return -1;
return levels[(k-1)];

}
};

you could replace vector to priority

analysis

  • time complexity O(nlogn)
  • space complexity O(n)