Advertisement
Guest User

669. Trim a Binary Search Tree

a guest
Aug 26th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  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. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement