```
struct TreeNode* trimBST(struct TreeNode* root, int L, int R) {
bool change = 0;
if(root) {
if(root->val < L) {
root = root->right;
change = 1;
} else if (root->val > R) {
root = root->left;
change = 1;
}
if (change) {
root = trimBST(root, L, R);
}
if (root) {
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
}
}
return root;
}
```