def trimBST(tree, minVal, maxVal):
if not tree:
return
tree.left=trimBST(tree.left, minVal, maxVal)
tree.right=trimBST(tree.right, minVal, maxVal)
##########
# Shouldn’t there be a check here on tree.left == None and tree.right == None
# before the comparisons below? This happens when leaf nodes are checked and
# the first line returns None viz:
#
# if not tree:
# return
##########
if minVal<=tree.val<=maxVal:
return tree
if tree.valmaxVal:
return tree.left