오늘 대학교 마지막 시험 끝!
이제부터 새롭게 시작, 모두 다 힘차게 마무리했으면 좋겠다.
# 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: Optional[TreeNode], low: int, high: int) -> int:
if not root:
return 0
if root.val < low:
return self.rangeSumBST(root.right, low, high)
elif root.val > high:
return self.rangeSumBST(root.left, low, high)
return root.val + self.rangeSumBST(root.right, low, high) + self.rangeSumBST(root.left, low, high)
Recursion을 이용한 BST sum 구하기!
누군가에게는 끝이 다른 이에게는 시작임을 :)
'Leetcode' 카테고리의 다른 글
Leetcode 2160. Minimum Sum of Four Digit Number After Splitting Digits (0) | 2022.02.11 |
---|---|
Leetcode 532. K-diff Pairs in an Array (0) | 2022.02.10 |
Leetcode 26. Remove Duplicates from Sorted Array (0) | 2021.12.12 |
Leetcode 563. Binary Tree Tilt (0) | 2021.12.09 |
Leetcode 268. Missing Number (0) | 2021.11.23 |