1. Code
class Solution:
def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
def getNode(node):
# this is a break condition
if node == None:
return
# when the value is in the range
elif node.val >= low and node.val <= high:
vals.append(node.val)
getNode(node.left)
getNode(node.right)
# if the value is less than low, the right node of the node should be checked
elif node.val < low:
getNode(node.right)
# if the value is greater than high, the left node of the node should be checked
elif node.val > high:
getNode(node.left)
vals = []
getNode(root)
return sum(vals)
2. Result
Runtime : 196 ms(94.68%), Memory usage : 22.4 MB(18.98%)
(Runtime can be different by a system even if it is a same code.)