일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 매일
- 사이드
- 파비최
- Writing
- 10분
- 운동
- FIT XR
- 미드시청
- Daily Challenge
- 개발자
- 영어공부
- 영어원서읽기
- English
- 잡생각
- 30분
- 스탭퍼
- 읽기
- leetcode
- Problem Solving
- 링피트
- 괜찮음
- 월간
- 만화도
- 뭐든
- 리얼 클래스
- 프로젝트
- 쓰릴오브파이트
- 3줄정리
- 화상영어
- realclass
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.03.14 Today's Challenge 본문
https://leetcode.com/problems/sum-root-to-leaf-numbers/
Sum Root to Leaf Numbers - LeetCode
Can you solve this real interview question? Sum Root to Leaf Numbers - You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. * For example, the root-to-leaf path 1 -> 2 -> 3 repr
leetcode.com
전형적인 dfs 문제
class Solution:
def sumNumbers(self, root: Optional[TreeNode]) -> int:
self.result = 0
def dfs(node, curr_sum):
if not node:
return
curr_sum = curr_sum * 10 + node.val
if not node.left and not node.right:
self.result += curr_sum
return
dfs(node.left, curr_sum)
dfs(node.right, curr_sum)
dfs(root, 0)
return self.result
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.03.16 Today's Challenge (0) | 2023.03.16 |
---|---|
2023.03.15 Today's Challenge (0) | 2023.03.15 |
2023.03.13 Today's Challenge (0) | 2023.03.13 |
2023.03.12 Today's Challenge (0) | 2023.03.12 |
2023.03.11 Today's Challenge (0) | 2023.03.11 |
Comments