forked from criszhou/LeetCode-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113. Path Sum II.py
31 lines (27 loc) · 918 Bytes
/
113. 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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def pathSumHelper(self, root, Sum, partialRes, results):
if root is None:
return
if root.left is None and root.right is None:
if root.val == Sum:
results.append( partialRes + [root.val] )
return
partialRes.append( root.val )
self.pathSumHelper(root.left, Sum-root.val, partialRes, results)
self.pathSumHelper(root.right, Sum-root.val, partialRes, results)
partialRes.pop()
def pathSum(self, root, Sum):
"""
:type root: TreeNode
:type Sum: int
:rtype: List[List[int]]
"""
ret = []
self.pathSumHelper(root, Sum, partialRes=[], results=ret)
return ret