-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3319-k-th-largest-perfect-subtree-size-in-binary-tree.cpp
More file actions
41 lines (40 loc) · 1.32 KB
/
3319-k-th-largest-perfect-subtree-size-in-binary-tree.cpp
File metadata and controls
41 lines (40 loc) · 1.32 KB
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
38
39
40
41
/**
* 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:
int kthLargestPerfectSubtree(TreeNode* root, int k) {
vector<int> a;
//return (size, isperfect)
auto dfs = [&](auto &dfs, TreeNode* root) -> pair<int ,int> {
//null is size 0 and perfect
if (root == nullptr) {
return {0, 1};
}
//leaf is size 1 and perfect
if (root -> left == nullptr && root -> right == nullptr) {
a.push_back(1);
return {1, 1};
}
auto [sz_left, pf_left] = dfs(dfs, root -> left);
auto [sz_right, pf_right] = dfs(dfs, root -> right);
bool pf = pf_left && pf_right && (sz_left == sz_right);
int sz = 1 + sz_left + sz_right;
if (pf) {
a.push_back(sz);
}
return {sz, pf};
};
dfs(dfs, root);
sort(a.rbegin(), a.rend());
return a.size() >= k ? a[k - 1] : -1;
}
};