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

Leetcode 938. Range Sum of BST的更多相关文章

  1. LeetCode #938. Range Sum of BST 二叉搜索树的范围和

    https://leetcode-cn.com/problems/range-sum-of-bst/ 二叉树中序遍历 二叉搜索树性质:一个节点大于所有其左子树的节点,小于其所有右子树的节点 /** * ...

  2. 【Leetcode_easy】938. Range Sum of BST

    problem 938. Range Sum of BST 参考 1. Leetcode_easy_938. Range Sum of BST; 完

  3. 【LeetCode】938. Range Sum of BST 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  4. 938. Range Sum of BST

    Given the root node of a binary search tree, return the sum of values of all nodes with value betwee ...

  5. LeetCode--Jewels and Stones && Range Sum of BST (Easy)

    771. Jewels and Stones (Easy)# You're given strings J representing the types of stones that are jewe ...

  6. [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  7. [LeetCode] 304. Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  8. [Leetcode Week16]Range Sum Query - Mutable

    Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...

  9. [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

随机推荐

  1. python之元组操作

    1.元组创建 tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", &qu ...

  2. [WebException: The underlying connection was closed: The message length limit was exceeded.]解决方法

    [WebException: The underlying connection was closed: The message length limit was exceeded.]   Syste ...

  3. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  4. sql 转

  5. Python3.x:实现多任务(多进程)

    Python3.x:实现多任务(多进程) # python3 # author lizm # datetime 2018-02-13 16:00:00 # -*- coding: utf-8 -*- ...

  6. 响应式Tab选项卡

    在线演示 本地下载

  7. session的活化与钝化 (转)

    session的活化与钝化就是当用户访问时网站异常,不能丢掉session,所有也必须采用文件存储:和之前那个统计网站访问量一样的原理. class Person implements必须实现这两个接 ...

  8. Spring_事务(2)

  9. sql报字段过大的错误解决方法

    set global max_allowed_packet = 2*1024*1024*10

  10. Word Search, 在矩阵中寻找字符串,回溯算法

    问题描述: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...