Problem
- Leetcode 938: Range sum BST
- Level: Easy
- Link
My Approach
# 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 rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
queue = []
sum_of_node = 0
queue.append(root)
while True:
n = len(queue)
if n <= 0:
break
while n > 0:
node = queue.pop(0)
if low <= node.val <= high:
sum_of_node += node.val
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
n -= 1
return sum_of_node
'개발공부 > algorithm' 카테고리의 다른 글
[프로그래머스][python] 짝지어 제거하기 - stack/queue (0) | 2021.05.18 |
---|---|
[Leetcode][python] 1137. N-th Tribonacci Number - DP (0) | 2021.05.16 |
[Leetcode][python] 563. Binary Tree Tilt - DFS (0) | 2021.05.16 |
[Leetcode][python] 107: Binary Tree Level order Traversal II (0) | 2021.05.15 |
[프로그래머스][python] 프린터 - Stack/queue (0) | 2021.05.08 |