Re: [闲聊] 每日leetcode

楼主: sixB (6B)   2024-10-22 14:58:19
2583.
※ 引述《DJYOMIYAHINA (通通打死)》之铭言:
: 理论上好像应该BFS+size k的minheap
真的欸
space差好多ㄛ==
原本想说反正全部都要加
1e5而已 就给他开下去
dfs比较好写 对ㄚ
/**
* 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) {}
* };
*/
using ll = long long;
class Solution {
public:
vector<ll> lv_sum;
long long kthLargestLevelSum(TreeNode* root, int k) {
lv_sum = vector<ll>(1e5 + 7, 0);
int n = sum_up(0, root);
if(k > n) return -1;
lv_sum.resize(n);
ranges::sort(lv_sum);
return lv_sum[n-k];
}
int sum_up(int lv, TreeNode* cur){
if(cur == nullptr) return lv;
lv_sum[lv] += cur->val;
return max(sum_up(lv+1, cur->left), sum_up(lv+1, cur->right));
}
};
作者: DJYOMIYAHINA (通通打死)   2024-10-22 15:11:00
别卷了
作者: steven183 (steven183183)   2024-10-22 15:15:00
别卷了

Links booklink

Contact Us: admin [ a t ] ucptt.com