-
Notifications
You must be signed in to change notification settings - Fork 0
/
113.py
29 lines (27 loc) · 1.02 KB
/
113.py
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
# 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 dfs(self, path, path_list, root, target_sum):
if not root:
return
else:
path.append(root.val)
if not (root.left or root.right):
sum_ = sum(path.copy())
if sum_ == target_sum:
path_list.append(path.copy())
else:
self.dfs(path=path, path_list=path_list, root=root.left, target_sum=target_sum)
self.dfs(path=path, path_list=path_list, root=root.right, target_sum=target_sum)
path.pop()
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root:
return []
path = list()
path_list = list()
self.dfs(path=path, path_list=path_list, root=root, target_sum=targetSum)
return path_list