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
- 1주일회고
- 빈 충돌
- 단기개발자코스
- 빈 조회 2개 이상
- 프로그래머스 이중우선순위큐
- 구글 OAuth login
- Python
- Spring multimodule
- 디자인패턴
- 디자인 패턴
- @FeignClient
- 99클럽
- 프로그래머스
- 인프콘 2024
- KPT회고
- infcon 2024
- DesignPattern
- jwt
- 취업리부트코스
- 코딩테스트 준비
- TiL
- 개발자부트캠프추천
- 항해99
- 파이썬
- 커스텀 헤더
- 개발자 취업
- JavaScript
- jwttoken
- 전략패턴 #StrategyPattern #디자인패턴
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 700. Search in a Binary Search Tree with Python 본문
알고리즘 with python/알고리즘 스터디
LeetCode 700. Search in a Binary Search Tree with Python
정민됴 2024. 1. 12. 10:43https://leetcode.com/problems/search-in-a-binary-search-tree/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 searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
answer = []
def dfs(node, val):
if node.val == val:
answer.append(node)
elif node.val > val and node.left:
dfs(node.left, val)
elif node.val < val and node.right:
dfs(node.right, val)
dfs(root, val)
return answer[0] if answer else None
얘 또한 이진 탐색 트리여서 찾는 값보다 크면 왼쪽을 뒤지고 찾는 값보다 작으면 오른쪽을 뒤지는 코드이다!
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
99클럽 코테 스터디 1일차 TIL 해시 : 프로그래머스 LV.2 전화번호 목록 with Python (0) | 2024.05.20 |
---|---|
LeetCode 110. Balanced Binary Tree with Python (0) | 2024.01.12 |
LeetCode 938. Range Sum of BST 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 |