Trim a Binary Search Tree – LeetCode 669
Problem
Description
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.
Answer
Original
Code
1 | /** |
思路
简单的DFS删除,注意维护返回的根节点。时间复杂度$O(n)$,空间复杂度$O(n)$。
耗时$8$ ms,排名$1.46\%$
Better
思路
还没看到更好的思路