일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 괜찮음
- 프로젝트
- 잡생각
- leetcode
- Daily Challenge
- 운동
- 스탭퍼
- Problem Solving
- 10분
- 영어공부
- 30분
- 미드시청
- 사이드
- 화상영어
- English
- 만화도
- 리얼 클래스
- Writing
- 월간
- 뭐든
- 링피트
- FIT XR
- 매일
- 쓰릴오브파이트
- 3줄정리
- 개발자
- 영어원서읽기
- realclass
- 읽기
- 파비최
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/path-sum-ii/
Path Sum II - 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
DFS 를 이용해서 푸는 문제.
뭐든 트리는 Base condition 에 대해 생각하는게 중요한 듯.
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
res = []
def dfs(root, path, ans, rem_sum):
if not root:
return []
path.append(root.val)
if not root.left and not root.right and rem_sum == root.val:
res.append(list(path))
dfs(root.left, path, res, rem_sum - root.val)
dfs(root.right, path, res, rem_sum - root.val)
path.pop()
dfs(root, [], res, targetSum)
return res
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (0) | 2022.09.27 |
---|---|
Today's Challenge (0) | 2022.09.25 |
Today's Challenge (0) | 2022.09.23 |
Today's Challenge (0) | 2022.09.22 |
Today's Challenge (0) | 2022.09.21 |
Comments