forked from onlybooks/python-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path52-3.py
More file actions
22 lines (20 loc) · 641 Bytes
/
52-3.py
File metadata and controls
22 lines (20 loc) · 641 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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: TreeNode, L: int, R: int) -> int:
stack, sum = [root], 0
# 스택 이용 필요한 노드 DFS 반복
while stack:
node = stack.pop()
if node:
if node.val > L:
stack.append(node.left)
if node.val < R:
stack.append(node.right)
if L <= node.val <= R:
sum += node.val
return sum