-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0113M. Path Sum II.py
More file actions
27 lines (26 loc) · 1.02 KB
/
0113M. Path Sum II.py
File metadata and controls
27 lines (26 loc) · 1.02 KB
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
#Runtime: 36 ms, faster than 97.12% of Python3 online submissions for Path Sum II.
#Memory Usage: 15.6 MB, less than 65.47% of Python3 online submissions for Path Sum II.
# 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 root
cand = []
temp = []
def go_deep(node, targetSum, cand, temp):
temp.append(node.val)
if not node.left and not node.right:
if sum(temp) == targetSum:
cand += [temp[:]]
if node.left:
cand = go_deep(node.left, targetSum, cand, temp)
if node.right:
cand = go_deep(node.right, targetSum, cand, temp)
temp.pop()
return cand
return go_deep(root, targetSum, cand, temp)