forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0938.py
More file actions
26 lines (22 loc) · 672 Bytes
/
0938.py
File metadata and controls
26 lines (22 loc) · 672 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
26
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def rangeSumBST(self, root, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: int
"""
if not root or L > R:
return 0
if root.val < L:
return self.rangeSumBST(root.right, L, R)
if root.val > R:
return self.rangeSumBST(root.left, L, R)
return root.val + self.rangeSumBST(root.left, L, root.val - 1)+\
self.rangeSumBST(root.right, root.val + 1, R)