SHARE
TWEET

669. Trim a Binary Search Tree

a guest Aug 26th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Time Complexity: O(n), n is the number of nodes
  2. // Space Complexity: O(n), to create n functions
  3. class Solution {
  4. public:
  5.     TreeNode* trimBST(TreeNode* root, int L, int R) {
  6.         if (!root) {
  7.             return nullptr;
  8.         }
  9.         if (root->val < L) {
  10.             return trimBST(root->right, L, R);
  11.         }
  12.         if (root->val > R) {
  13.             return trimBST(root->left, L, R);
  14.         }
  15.         root->left = trimBST(root->left, L, R);
  16.         root->right = trimBST(root->right, L, R);
  17.         return root;
  18.     }
  19. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top