-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path938.py
More file actions
25 lines (24 loc) · 655 Bytes
/
Copy path938.py
File metadata and controls
25 lines (24 loc) · 655 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
25
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
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
sum = 0
if root.val <= R and root.val >= L:
sum += root.val
if root.val < R:
sum += self.rangeSumBST(root.right, L, R)
if root.val > L:
sum += self.rangeSumBST(root.left, L, R)
return sum