LeetCode 669. Trim a Binary Search Tree
撰写于 2018-01-03 修改于 2018-01-21 分类 LeetCode
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.
The problem is pretty simple, follow the following steps and use recursion method
- If the val of current node is smaller than L, abandon the left sub-tree and trim its right sub-tree
- If the val of current node is greater than R, abandon the right sub-tree and trim its left sub-tree
- Else, recursively trim its left and right sub-tree and return the root