-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113_path_sum_ii.py
More file actions
37 lines (29 loc) · 895 Bytes
/
113_path_sum_ii.py
File metadata and controls
37 lines (29 loc) · 895 Bytes
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
32
33
34
35
36
37
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def pathSum(self, root, targetSum):
"""
:type root: TreeNode
:type targetSum: int
:rtype: List[List[int]]
"""
if not root:
return []
self.result = []
def dfs(node, path):
if not node:
return
path.append(node.val)
if node.left is None and node.right is None:
if sum(path) == targetSum:
self.result.append(copy.deepcopy(path))
else:
dfs(node.left, path)
dfs(node.right, path)
path.pop()
dfs(root, [])
return self.result