-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-sum-ii.py
53 lines (50 loc) · 1.41 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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]]
"""
res = []
def recursion(root,stack,value):
if(root!=None and root.left==None and root.right==None):
if(value+root.val == sum):
stack.append(root.val)
res.append(list(stack))
return
if(value+root.val > sum):
return
if(root==None):
return
recursion(root.left, stack+[root.val], value+root.val)
recursion(root.right, stack+[root.val], value+root.val)
recursion(root,[],0)
return res
# 新的实现方式更高效
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: List[List[int]]
"""
if(not root):
return []
res = []
def dfs(root,arr,n):
if root and (not root.left) and (not root.right):
if(n+root.val == sum):
tmp = arr+[root.val]
res.append(list(tmp))
return
if root.left:
dfs(root.left,arr+[root.val],n+root.val)
if root.right:
dfs(root.right,arr+[root.val],n+root.val)
dfs(root,[],0)
return res