Skip to content

Commit

Permalink
二分搜索树区间和
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Nov 11, 2018
1 parent e0c98dd commit 9beda79
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lc938. Range Sum of BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

from bisect import bisect_left


class Solution:
def dfs(self, root, lst):
if not root:
return
self.dfs(root.left, lst)
lst.append(root.val)
self.dfs(root.right, lst)

def rangeSumBST(self, root, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: int
"""
lst = []
self.dfs(root, lst)
lo = bisect_left(lst, L)
hi = bisect_left(lst, R)
return sum(lst[lo:hi + 1])


root = TreeNode(10)
root.left = TreeNode(5)
root.right = TreeNode(15)
root.left.left = TreeNode(3)
root.left.right = TreeNode(7)
L = 7
R = 15
s = Solution()
print(s.rangeSumBST(root, L, R))

0 comments on commit 9beda79

Please sign in to comment.