-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1080.sufficientSubset.cpp
34 lines (32 loc) · 1019 Bytes
/
1080.sufficientSubset.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
33
34
#include <bits/stdc++.h>
#include "common_head.h"
using namespace std;
class Solution {
public:
bool checkSufficientLeaf(TreeNode *node, int sum, int limit) {
if (!node) {
return false;
}
if (node->left == nullptr && node->right == nullptr) {
return node->val + sum >= limit;
}
bool haveSufficientLeft = checkSufficientLeaf(node->left, sum + node->val, limit);
bool haveSufficientRight = checkSufficientLeaf(node->right, sum + node->val, limit);
if (!haveSufficientLeft) {
node->left = nullptr;
}
if (!haveSufficientRight) {
node->right = nullptr;
}
return haveSufficientLeft || haveSufficientRight;
}
TreeNode *sufficientSubset(TreeNode *root, int limit) {
bool haveSufficient = checkSufficientLeaf(root, 0, limit);
return haveSufficient ? root : nullptr;
}
};
TreeNode *sufficientSubset(TreeNode *root, int limit) {
}
int main() {
return 0;
}