-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0113-path-sum-ii.py
More file actions
26 lines (21 loc) · 902 Bytes
/
Copy path0113-path-sum-ii.py
File metadata and controls
26 lines (21 loc) · 902 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
26
# https://leetcode.com/problems/path-sum-ii/submissions/
# O(n) - Time
# O(lgn) - Space (recursion depth)
# 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]]:
output = []
def helper(root, res, resSum, target):
if not root: return
if resSum + root.val == target and not root.left and not root.right:
output.append(res + [root.val])
return
helper(root.left, res + [root.val], resSum + root.val, target)
helper(root.right, res + [root.val], resSum + root.val, target)
helper(root, [], 0, targetSum)
return output