forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0113.py
More file actions
23 lines (19 loc) · 718 Bytes
/
0113.py
File metadata and controls
23 lines (19 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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]]:
ans = []
self.dfs(root, sum, [], ans)
return ans
def dfs(self, root: TreeNode, sum: int, curr: List[int], ans: List[List[int]]) -> None:
if not root:
return
if sum == root.val and not root.left and not root.right:
ans.append(curr + [root.val])
return
self.dfs(root.left, sum - root.val, curr + [root.val], ans)
self.dfs(root.right, sum - root.val, curr + [root.val], ans)