-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path669.cpp
62 lines (61 loc) · 1.73 KB
/
669.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
__________________________________________________________________________________________________
sample 12 ms submission
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(root == NULL) return NULL;
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
if(root->val < L) return root->right;
if(root->val > R) return root->left;
return root;
}
};
static auto speedup = [](){
ios::sync_with_stdio(false);
cin.tie(nullptr);
return nullptr;
}();
__________________________________________________________________________________________________
sample 13116 kb submission
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
// 0
// 1 2
TreeNode* helper(TreeNode* root, int L, int R) {
if(root) {
root->left = helper(root->left, L, R);
root->right = helper(root->right, L, R);
int val = root->val;
if(val < L || R < val) {
if(val < L) return root->right;
if(R < val) return root->left;
}
return root;
} else {
return nullptr;
}
}
TreeNode* trimBST(TreeNode* root, int L, int R) {
return helper(root, L, R);
}
};
__________________________________________________________________________________________________