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