Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- spring batch 5.0
- TiL
- jwttoken
- DesignPattern
- 전략패턴 #StrategyPattern #디자인패턴
- 구글 OAuth login
- 프로그래머스
- 개발자 취업
- 프로그래머스 이중우선순위큐
- KPT회고
- 디자인 패턴
- Spring multimodule
- 빈 충돌
- 인프콘 2024
- @FeignClient
- 취업리부트코스
- 디자인패턴
- 단기개발자코스
- JavaScript
- 개발자부트캠프추천
- infcon 2024
- jwt
- 커스텀 헤더
- 파이썬
- 코딩테스트 준비
- 항해99
- 1주일회고
- 99클럽
- 빈 조회 2개 이상
- Python
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 938. Range Sum of BST with Python 본문
https://leetcode.com/problems/range-sum-of-bst/description/
# 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:
answer = [0]
def dfs(node, low, high):
if not node:
return
# low 넘버보다 작은데 오른쪽 노드가 있으면 오른쪽으로 가기
if node.val < low and node.right:
dfs(node.right, low, high)
# high 넘버보다 큰데 왼쪽 노드가 있으면 왼쪽으로 가기
elif node.val > high and node.left:
dfs(node.left, low, high)
# 사이값이거나 같을 때 그 값을 더하고 양쪽 써칭
elif node.val >= low and node.val <= high:
answer[0] += node.val
dfs(node.left, low, high)
dfs(node.right, low, high)
dfs(root, low, high)
return answer[0]
이진탐색 트리였기 때문에 이진탐색하듯이 low보다 작아? 그럼 오른쪽으로 가 high 보다 커? 그럼 왼쪽으로 가
이런 식으로 써칭하고 만약에 해당하는 값이거나 사이값일 경우 양쪽 다 확인해! 하는 문제였다!
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
LeetCode 110. Balanced Binary Tree with Python (0) | 2024.01.12 |
---|---|
LeetCode 700. Search in a Binary Search Tree with Python (0) | 2024.01.12 |
LeetCode 543. Diameter of Binary Tree with Python (0) | 2024.01.12 |
LeetCode 207. Course Schedule with Python (0) | 2024.01.11 |
LeetCode 78. Subsets with Python (0) | 2024.01.11 |