일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- kaggle
- Python Implementation
- 파이썬
- iterator
- 컴퓨터의 구조
- Substring with Concatenation of All Words
- data science
- 프로그래머스
- 315. Count of Smaller Numbers After Self
- t1
- 109. Convert Sorted List to Binary Search Tree
- 밴픽
- Python Code
- Protocol
- Python
- Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- DWG
- concurrency
- attribute
- 715. Range Module
- 43. Multiply Strings
- 운영체제
- Class
- shiba
- Generator
- LeetCode
- 시바견
- Decorator
- Regular Expression
Archives
- Today
- Total
Scribbling
LeetCode: 113. Path Sum II 본문
DFS Solution
# 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 pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root:
return []
self.ret = []
self.dfs(root, [], 0, targetSum)
return self.ret
def dfs(self, node, path, tempSum, targetSum):
if not node.left and not node.right:
if tempSum + node.val == targetSum:
self.ret.append(path+[node.val])
return
if node.left:
self.dfs(node.left, path+[node.val], tempSum+node.val, targetSum)
if node.right:
self.dfs(node.right, path+[node.val], tempSum+node.val, targetSum)
'Computer Science > Coding Test' 카테고리의 다른 글
프로그래머스: 타겟 넘버 (0) | 2021.10.31 |
---|---|
LeetCode: 115. Distinct Subsequences (0) | 2021.10.27 |
LeetCode: 109. Convert Sorted List to Binary Search Tree (0) | 2021.10.27 |
프로그래머스: 도둑질 (0) | 2021.10.26 |
프로그래머스: 등굣길 (0) | 2021.10.26 |