-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113_Path_Sum_II.py
More file actions
36 lines (34 loc) · 913 Bytes
/
113_Path_Sum_II.py
File metadata and controls
36 lines (34 loc) · 913 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
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]]
"""
self.cur = []
self.nums = []
self.sum = sum
if not root:
return []
self.dfs(root)
return self.nums
def dfs(self, node):
self.cur.append(node.val)
left, right = False, False
if node.left:
left = True
self.dfs(node.left)
if node.right:
right = True
self.dfs(node.right)
if not left and not right:
s = sum(self.cur)
if s == self.sum:
self.nums.append(self.cur[:])
self.cur.pop()