方法1
经典divide-and-conquer,不用考虑细节,直接dfs left, dfs right, 只需要考虑root和left / right的关系
class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
if not root:
return None
if root.val > high:
return self.trimBST(root.left, low, high)
elif root.val < low:
return self.trimBST(root.right, low, high)
else:
left = self.trimBST(root.left, low, high)
right = self.trimBST(root.right, low, high)
root.left = left
root.right = right
return root