-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathSolution.py
More file actions
27 lines (25 loc) · 816 Bytes
/
Solution.py
File metadata and controls
27 lines (25 loc) · 816 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
#coding=utf-8
__author__ = 'xuxuan'
# 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]]
"""
return list(self.f(root,[],sum))
def f(self,root,state,sum):
if root:
if root.left or root.right:
for result in self.f(root.left,state+[root.val],sum-root.val):
yield [root.val]+result
for result in self.f(root.right,state+[root.val],sum-root.val):
yield [root.val]+result
elif sum==root.val:
yield [root.val]