Path Sum III
Given the root of a binary tree and an integer targetSum, count the number of downward paths whose node values sum to targetSum. A path must go from a parent to a child (top to bottom) but need not start at the root or end at a leaf.
Open official problem prompt ↗Count how many contiguous top-to-bottom chains of nodes add up to a target value.
Walking down a trail while noting your total elevation gain at each marker. To find any stretch that gained exactly 8 meters, you check whether some earlier marker was exactly 8 below your current total.
- Input
- root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
- Output
- 3
- Why
- The paths 5->3, 5->2->1, and -3->11 each sum to 8.
The number of nodes is in the range [0, 1000]-10^9 <= Node.val <= 10^9-1000 <= targetSum <= 1000