669. Trim a Binary Search Tree
Recursive solution: Use the special property of binary search tree: left sons are smaller while right sons are greater.
Given a binary search tree and the lowest and highest boundaries as L
and R
, trim the tree so that all its elements lies in [L, R]
(R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Soulution:
1 | class Solution: |