-
Notifications
You must be signed in to change notification settings - Fork 0
/
113_path-sum-ii.py
36 lines (32 loc) · 936 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
32
33
34
35
36
# 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 pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: List[List[int]]
"""
if root:
val = root.val
if val == sum and root.left == root.right == None:
return [[val]]
else:
left = self.pathSum(root.left, sum - val)
right = self.pathSum(root.right, sum - val)
current = []
for c in left + right:
c.insert(0, val)
current.append(c)
return current
return []
p = TreeNode(1)
p.left = TreeNode(9)
p.right = n = TreeNode(20)
n.left = TreeNode(15)
n.right = TreeNode(7)
print(Solution().pathSum(p, 36))