669. Trim a Binary Search Tree
/**
* Definition for a binary tree node.
* 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) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(!root) return root;
if(root->left) root->left = trimBST(root->left, low, high);
if(root->right) root->right = trimBST(root->right, low, high);
if(root->val < low) {
root = root->right;
root = trimBST(root, low, high);
}
else if(root->val > high) {
root = root->left;
root = trimBST(root, low, high);
}
return root;
}
};