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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2022-01-25
猜你喜欢
  • 2021-10-16
  • 2021-12-25
  • 2021-08-07
  • 2021-10-03
  • 2021-12-01
相关资源
相似解决方案