Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. TreeNode *trimBST(TreeNode *root, int L, int R) {
  13. if (root == nullptr) {
  14. return nullptr;
  15. }
  16. if (root->val < L) {
  17. return trimBST(root->right, L, R);
  18. }
  19. if (root->val > R) {
  20. return trimBST(root->left, L, R);
  21. }
  22. if (root->val >= L && root->val <= R) {
  23. root->left = trimBST(root->left, L, R);
  24. root->right = trimBST(root->right, L, R);
  25. return root;
  26. }
  27. return nullptr;
  28. }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement