You are given the root of a binary tree and an integer targetSum. Your task is to find all root-to-leaf paths where the sum of the node values along the path equals targetSum.
A valid path must:
Return a list containing all such paths.
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: Two root-to-leaf paths sum to 22: 5 → 4 → 11 → 2 and 5 → 8 → 4 → 5.
Input: root = [1,2,3], targetSum = 5
Output: []
Explanation: No root-to-leaf path has a sum equal to 5.
Input: root = [], targetSum = 0
Output: []
Explanation: An empty tree has no paths.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 3 4 5 6 7
11
10 5 15 2 7 12 20
45