-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#113 pathSum.py
27 lines (22 loc) · 889 Bytes
/
#113 pathSum.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root:
return []
ans = []
self.findSum(root, [root.val], targetSum, ans)
return ans
def findSum(self, root: Optional[TreeNode], path: List[int], targetSum: int,
ans: List[List[int]]) -> None:
if sum(path) == targetSum and not root.left and not root.right:
ans.append(path)
else:
if root.left:
self.findSum(root.left, path + [root.left.val], targetSum, ans)
if root.right:
self.findSum(root.right, path + [root.right.val], targetSum, ans)