-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113PathSumII.py
36 lines (33 loc) · 1.23 KB
/
113PathSumII.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
def dfs(root ,sum , temp , ans):
if not root:
return
if root.left == None and root.right == None and root.val == sum:
temp.append(root.val)
ans.append(temp)
dfs(root.left , sum - root.val , temp + [root.val], ans)
dfs(root.right , sum - root.val , temp + [root.val] , ans)
ans = []
dfs(root, sum , [] , ans)
return ans
# 非递归
# def pathSum(self, root: TreeNode, sum_: int) -> List[List[int]]:
# if not root: return []
# stack = [([root.val], root)]
# res = []
# while stack:
# tmp, node = stack.pop()
# if not node.right and not node.left and sum(tmp) == sum_:
# res.append(tmp)
# if node.right:
# stack.append((tmp + [node.right.val], node.right))
# if node.left:
# stack.append((tmp + [node.left.val], node.left))
# return res