| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 29 | 30 | 31 |
Tags
- 뭐든
- 읽기
- 잡생각
- 화상영어
- FIT XR
- Problem Solving
- realclass
- 30분
- 매일
- 파비최
- 월간
- 사이드
- 개발자
- Writing
- 만화도
- 리얼 클래스
- 괜찮음
- 영어공부
- 운동
- 스탭퍼
- English
- 3줄정리
- 프로젝트
- 10분
- leetcode
- 링피트
- 쓰릴오브파이트
- 영어원서읽기
- Daily Challenge
- 미드시청
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
Lowest Common Ancestor of a Binary Search Tree - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
요즘 트리 문제가 많이 나옴.
순회 하면서 가장 적절한 root를 찾아서 리턴하면 되는 문제
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if (root == p or root == q or not root):
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if left and right:
return root
elif left:
return left
elif right:
return right반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
| Today's Challenge (0) | 2022.08.14 |
|---|---|
| Today's Challenge (0) | 2022.08.13 |
| Today's Challenge (0) | 2022.08.11 |
| Today's Challenge (0) | 2022.08.10 |
| Today's Challenge (0) | 2022.08.09 |
Comments