Path Sum III
1 approach, code in every language
Problem description
You are given the root of a binary tree and an integer targetSum. Count the number of paths whose node values add up to targetSum.
The path does not have to start at the root or end at a leaf, but it must travel strictly downward: it always moves from a parent node to one of its children. Two paths are considered different if they cover a different set of nodes.
Return the total count of such downward paths.
Example 1
Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
Output: 3
Three downward paths sum to 8: 5 -> 3, 5 -> 2 -> 1, and -3 -> 11.
Example 2
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: 3
There are three downward paths whose node values total 22.
Constraints
The number of nodes in the tree is in the range [0, 1000].-10^9 <= Node.val <= 10^9-1000 <= targetSum <= 1000
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Pipeline Stretch Tallies is our own problem on the technique behind Path Sum III: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free