Skip to main content

113 - Path Sum II

Details

KeyValue
Linkhttps://leetcode.com/problems/path-sum-ii/submissions/
LanguagePython 3
Runtime51 ms, faster than 87.32% of Python3 online submissions for Path Sum II
Memory Usage15.6 MB, less than 72.84% of Python3 online submissions for Path Sum II
DatastructuresTreeNode
AlgorithmsDFS
ComplexityTime: O(N^2) Memory: O(N)

Procedure

  1. ...

Code

# 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]]:

def dfs(node: TreeNode, targetSum: int):
if not node: return []
if not node.left and not node.right and targetSum == node.val:
return [[node.val]]

left = dfs(node.left, targetSum - node.val)
right = dfs(node.right, targetSum - node.val)

return [cand + [node.val] for cand in left + right]

return [result[::-1] for result in dfs(root, targetSum)]