# # Definition for a binary tree node.
# # class TreeNode:
# # def __init__(self, val=0, left=None, right=None):
# # self.val = val
# # self.left = left
# # self.right = right
class Solution:
def trimBST(self, root: Optional[TreeNode], L: int, R: int) -> Optional[TreeNode]:
if root is None:
return None
while root and (root.val < L or root.val > R):
if root.val < L:
root = root.right
else:
root = root.left
cur = root
while cur:
while cur.left and cur.left.val < L:
cur.left = cur.left.right
cur = cur.left
cur = root
while cur:
while cur.right and cur.right.val > R:
cur.right = cur.right.left
cur = cur.right
return root
669. 修剪二叉搜索树
最新推荐文章于 2024-06-29 16:57:48 发布