Trim a Binary Search Tree
Last updated
Was this helpful?
Last updated
Was this helpful?
Given a binary search tree and the lowest and highest boundaries as
L
andR
, 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.
用经典的分治解法即可。假设左右子树都已完成剪枝,对当前root节点的处理要用到二叉搜索树的性质。
时间复杂度为所有树节点树。还可以做一些小优化,比如把对root判断条件放到前面从而减少需要检查的节点。