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