-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113.path-sum-ii.py
More file actions
25 lines (20 loc) · 809 Bytes
/
113.path-sum-ii.py
File metadata and controls
25 lines (20 loc) · 809 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
# 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: TreeNode, sum: int) -> List[List[int]]:
paths = list()
self.findPath(root, sum, list(), paths)
return paths
def findPath(self, root, sum, current, paths):
if root == None:
return
current.append(root.val)
if root.val == sum and root.left == None and root.right == None:
paths.append(current)
return
self.findPath(root.left, sum - root.val, list(current), paths)
self.findPath(root.right, sum - root.val, list(current), paths)