Path Sum II
MediumP2Coding
Given the root of a binary tree and an integer targetSum, return every root-to-leaf path whose values add up to targetSum. Each path is a list of node values from the root down. A leaf has no children.
List the paths in the order a depth-first search finds them, visiting the left child before the right child. Return [] if there is none.
Example 1
- 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]]
Example 2
- Input
- root = [1,2,3], targetSum = 5
- Output
- []
Constraints
- 0 <= number of nodes <= 5000
- -1000 <= Node.val <= 1000
- -1000 <= targetSum <= 1000
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.