일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Python Implementation
- 30. Substring with Concatenation of All Words
- 43. Multiply Strings
- Substring with Concatenation of All Words
- concurrency
- Decorator
- 시바견
- Class
- attribute
- Python Code
- 운영체제
- Protocol
- 밴픽
- DWG
- kaggle
- 109. Convert Sorted List to Binary Search Tree
- 파이썬
- Regular Expression
- Generator
- LeetCode
- Python
- shiba
- t1
- Convert Sorted List to Binary Search Tree
- 프로그래머스
- 315. Count of Smaller Numbers After Self
- 컴퓨터의 구조
- data science
- 715. Range Module
- iterator
Archives
- Today
- Total
Scribbling
LeetCode: 129. Sum Root to Leaf Numbers 본문
DFS로 푼다.
# 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 sumNumbers(self, root: Optional[TreeNode]) -> int:
self.sum = 0
self.dfs(root, '0')
return self.sum
def dfs(self, node, num: str):
if node.left == None and node.right == None:
self.sum += int(num + str(node.val))
return
if node.left != None:
self.dfs(node.left, num+str(node.val))
if node.right != None:
self.dfs(node.right, num+str(node.val))
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 146. LRU Cache (0) | 2021.11.11 |
---|---|
LeetCode: 130. Surrounded Regions (0) | 2021.11.10 |
프로그래머스: 오픈채팅방 (0) | 2021.11.08 |
LeetCode: 128. Longest Consecutive Sequence (0) | 2021.11.08 |
프로그래머스: 방의 개수 (0) | 2021.11.08 |