-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_938.py
More file actions
39 lines (35 loc) · 1.04 KB
/
LeetCode_938.py
File metadata and controls
39 lines (35 loc) · 1.04 KB
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
# 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
# elif root.val in range(L, R + 1):
# return root.val + self.rangeSumBST(root.left, L, R) + self.rangeSumBST(root.right, L, R)
# elif root.val < L:
# return self.rangeSumBST(root.right, L, R)
# else:
# return self.rangeSumBST(root.left, L, R)
if not root:
return 0
ans = 0
stack = [root]
while len(stack) > 0:
p = stack.pop()
if p.val >= L and p.val <= R:
ans += p.val
if p.left is not None:
stack.append(p.left)
if p.right is not None:
stack.append(p.right)
return ans