-
Notifications
You must be signed in to change notification settings - Fork 0
/
rangesum_bst.py
43 lines (40 loc) · 1.07 KB
/
rangesum_bst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
ans=None
def f(root,low,high):
if root is None :
return 0
if root.val <=high and root.val>=low:
global ans
ans+=root.val
f(root.left,low,high)
f(root.right,low,high)
def solve(root, low, high):
global ans
ans=0
f(root,low,high)
return ans
###############################################
def solve(root, low, high):
sum_=0
def f(root):
if root:
if root.val > low :
f(root.left)
if root.val >=low and root.val<= high :
nonlocal sum_
sum_+=root.val
if root.val <high:
f(root.right)
f(root)
return sum_
###############################################
class Solution:
def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int:
self.ans=0
def f(root):
if root:
f(root.left)
if low <= root.val and high >= root.val:
self.ans += root.val
f(root.right)
f(root)
return self.ans