1
class Solution {
2
public:
3
TreeNode *trimBST(TreeNode *root, int low, int high) {
4
if (!root) return NULL;
5

6
root->left = trimBST(root->left, low, high);
7
root->right = trimBST(root->right, low, high);
8

9
if (root->val < low) {
10
if (root->right) {
11
TreeNode *temp = root;
12
// delete root;
13
return temp->right;
14
} else {
15
return NULL;
16
}
17
}
18
if (root->val > high) {
19
if (root->left) {
20
TreeNode *temp = root;
21
// delete root;
22
return temp->left;
23
} else {
24
return NULL;
25
}
26
}
27
return root;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0