-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0113.py
More file actions
25 lines (20 loc) · 735 Bytes
/
0113.py
File metadata and controls
25 lines (20 loc) · 735 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
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root:
return []
global results
results = []
s = []
def traverse(node, s):
if node.right:
s.append(node.val)
traverse(node.right, s)
s.pop(-1)
if node.left:
s.append(node.val)
traverse(node.left, s)
s.pop(-1)
if not node.right and not node.left and sum(s) + node.val == targetSum:
results.append(s + [node.val])
traverse(root, s)
return results