import functools
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
@functools.lru_cache()
class Solution(object):
def rangeSumBST(self, root, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: int
"""
if not root:
return 0
return self.compare(root.val, L, R) + self.rangeSumBST(root.left, L, R) + self.rangeSumBST(root.right, L, R)
def compare(self, val, L, R):
return val if L <= val <= R else 0