-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
39 lines (31 loc) · 1005 Bytes
/
main.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
37
38
39
# Definition for a binary tree node.
from typing import List
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: TreeNode, targetSum: int) -> List[List[int]]:
res = []
self.dfs(root, targetSum, 0, res, [])
return res
def dfs(self, node: TreeNode, targetSum: int, sum: int, res: List[List[int]], nums: List[int]) -> None:
if not node:
return
if node.left == node.right and sum + node.val == targetSum:
nums.append(node.val)
res.append(nums.copy())
nums.pop()
nums.append(node.val)
self.dfs(node.left, targetSum, sum + node.val, res, nums)
self.dfs(node.right, targetSum, sum + node.val, res, nums)
nums.pop()
sol = Solution()
a = TreeNode(1)
b = TreeNode(2)
c = TreeNode(3)
a.left = b
a.right = c
target = 3
print(sol.pathSum(a, target))