-
Notifications
You must be signed in to change notification settings - Fork 0
/
2583. 二叉树中的第 K 大层和.cpp
32 lines (31 loc) · 989 Bytes
/
2583. 二叉树中的第 K 大层和.cpp
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
/**
* 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:
void solve(TreeNode* root ,int level, unordered_map<int,long long>&mapping){
if(root==NULL) return;
solve(root->left,level+1,mapping);
mapping[level]+=root->val;
solve(root->right,level+1,mapping);
}
long long kthLargestLevelSum(TreeNode* root, int k) {
unordered_map<int,long long >mapping;
solve(root,0,mapping);
vector<long long>ans;
for(auto i : mapping){
ans.push_back(i.second);
}
sort(ans.begin(),ans.end());
if(k>ans.size()) return -1;
return ans[ans.size()-k];
}
};