# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def rangeSumBST(self, root, L, R):
        """
        :type root: TreeNode
        :type L: int
        :type R: int
        :rtype: int
        """
        def helper(self, root, sofar):
            if not root:
                return sofar
            if root.val < L:
                return helper(self, root.right, sofar)
            elif root.val > R:
                return helper(self, root.left, sofar)
            else:
                left = helper(self, root.left, sofar + root.val)
                return helper(self, root.right, left)
        return helper(self, root, 0)

        