Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created April 24, 2018 14:11
Show Gist options
  • Save s4553711/d6e9e428cdade499ed6885e6222f0223 to your computer and use it in GitHub Desktop.
Save s4553711/d6e9e428cdade499ed6885e6222f0223 to your computer and use it in GitHub Desktop.
/**
* 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) return nullptr;
if (root->val < L) return trimBST(root->right, L, R);
if (root->val > R) return trimBST(root->left, L, R);
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
return root;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment