Loading content...
You are given the root of a binary tree and an integer representing a target sum. Your task is to discover and return all complete paths from the root node to any leaf node where the cumulative sum of all node values along the path equals the specified target.
A complete path must start at the root and terminate at a leaf node—a node that has no children (neither left nor right child). Each path should be represented as an ordered list of node values encountered while traversing from root to leaf.
Return all such paths as a list of lists. If no valid paths exist that satisfy the target sum condition, return an empty list. The order of the paths in the result does not matter.
root = [5,4,8,11,null,13,4,7,2,null,null,5,1]
targetSum = 22[[5,4,11,2],[5,8,4,5]]There are two distinct root-to-leaf paths with a cumulative sum of 22: • Path 1: 5 → 4 → 11 → 2 (sum: 5 + 4 + 11 + 2 = 22) • Path 2: 5 → 8 → 4 → 5 (sum: 5 + 8 + 4 + 5 = 22) Both paths start at the root (5) and end at leaf nodes (2 and 5 respectively).
root = [1,2,3]
targetSum = 5[]The tree has two root-to-leaf paths: • 1 → 2 (sum: 3) • 1 → 3 (sum: 4) Neither path yields the target sum of 5, so we return an empty list.
root = [1,2]
targetSum = 0[]The only root-to-leaf path is 1 → 2, which sums to 3. Since no path equals the target sum of 0, we return an empty list.
Constraints