-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleet_669.cpp
63 lines (61 loc) · 1.39 KB
/
leet_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
63
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#define MIN(X,Y) ((X) < (Y) ? : (X) : (Y))
#define MAX(X,Y) ((X) > (Y) ? : (X) : (Y))
using namespace std;
const int N=100001;
int n,m;
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) {}
};
TreeNode* trimLeft(TreeNode * root, int L){
while(root != nullptr && root -> val < L){
root = root -> right;
}
if(root == nullptr) return root;
TreeNode * prev = root;
TreeNode * cur = root -> left;
while(cur != nullptr){
if (cur -> val >= L){
prev = cur;
cur = cur -> left;
} else{
prev -> left = cur -> right;
cur = cur -> right;
}
}
return root;
}
TreeNode* trimRight(TreeNode * root, int R){
while(root != nullptr && root -> val > R){
root = root -> left;
}
if(root == nullptr) return root;
TreeNode * prev = root;
TreeNode * cur = root -> right;
while(cur != nullptr){
if (cur -> val <= R){
prev = cur;
cur = cur -> right;
} else{
prev -> right = cur -> left;
cur = cur -> left;
}
}
return root;
}
TreeNode* trimBST(TreeNode* root, int L, int R) {
root = trimLeft(root,L);
root = trimRight(root,R);
return root;
}
int main(void) {
scanf("%d%d", &n, &m);
}