| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- concurrency
- DWG
- 파이썬
- Python
- kaggle
- Convert Sorted List to Binary Search Tree
- Regular Expression
- shiba
- Python Code
- t1
- Decorator
- 시바견
- 315. Count of Smaller Numbers After Self
- 밴픽
- 컴퓨터의 구조
- attribute
- data science
- 109. Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- Generator
- 운영체제
- Class
- 715. Range Module
- Python Implementation
- Protocol
- iterator
- 30. Substring with Concatenation of All Words
- LeetCode
- 프로그래머스
- Substring with Concatenation of All Words
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 |