-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113.py
33 lines (32 loc) · 942 Bytes
/
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
29
30
31
32
33
# 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 not root:
return []
if root.right == None and root.left == None:
if root.val == sum:
return [[root.val]]
else:
return []
res = []
if root.left:
temp = self.pathSum(root.left, sum - root.val)
if temp:
for i in temp:
res.append([root.val] + i)
if root.right:
temp = self.pathSum(root.right, sum - root.val)
if temp:
for i in temp:
res.append([root.val] + i)
return res