-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-sum-ii.py
36 lines (27 loc) · 904 Bytes
/
path-sum-ii.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
29
30
31
32
33
34
35
36
# 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 []
result = []
self.helper(root, targetSum, [], result)
return result
def helper(self, node, targetSum, path, result):
if not node:
return
path.append(node.val)
if not node.left and not node.right and node.val == targetSum:
result.append(path[:])
self.helper(node.left, targetSum - node.val, path, result)
self.helper(node.right, targetSum - node.val, path, result)
path.pop()