-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path938. Range Sum of BST.py
More file actions
24 lines (22 loc) · 891 Bytes
/
938. Range Sum of BST.py
File metadata and controls
24 lines (22 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def getBSTNodeValues(self, root: TreeNode, runnigSum: int, l: int, r: int) -> None:
if root is None:
return
if root.val < l:
self.getBSTNodeValues(root.right, runnigSum, l, r)
elif root.val > r:
self.getBSTNodeValues(root.left, runnigSum, l, r)
else:
runnigSum[0] += root.val
self.getBSTNodeValues(root.right, runnigSum, l, r)
self.getBSTNodeValues(root.left, runnigSum, l, r)
def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:
runnigSum = [0]
self.getBSTNodeValues(root, runnigSum, L, R)
return runnigSum[0]