本题注意点:if root.val<L,直接return即可
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def trimBST(self, root, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: TreeNode
"""
if root is None: return None
if root.val < L:
return self.trimBST(root.right, L, R)
if root.val > R:
return self.trimBST(root.left, L, R)
root.left = self.trimBST(root.left, L, R)
root.right = self.trimBST(root.right, L, R)
return root
# version2:(我)
# if root is None: return None
# while(root.val < L or root.val > R):
# if root.val < L:
# root = root.right
# if root.val > R:
# root = root.left
# trimBST(root.left, L, R)
# trimBST(root.right, L, R)
# return root