Leetcode 938. Range Sum of BST

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

 

posted @ 2019-03-23 02:46  周洋  阅读(245)  评论(0编辑  收藏  举报