-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path113_Path_Sum_II.py
More file actions
28 lines (26 loc) · 851 Bytes
/
Copy path113_Path_Sum_II.py
File metadata and controls
28 lines (26 loc) · 851 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
28
# 2015-11-11 Runtime: 88 ms
# 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]]
"""
def dfs(node, Sum, oneAnswer):
if not node:
return
if not node.left and not node.right:
if Sum == node.val:
result.append(oneAnswer + [node.val])
return
dfs(node.left, Sum - node.val, oneAnswer + [node.val])
dfs(node.right, Sum - node.val, oneAnswer + [node.val])
result = []
dfs(root, sum, [])
return result