You're given the root of a binary tree and an integer targetSum. Your mission is to find the total number of downward paths in the tree where the sum of node values equals the target sum.
Here's the twist: paths don't need to start at the root or end at a leaf! Any downward path (parent to child) that sums to the target counts. Think of it like finding all the "treasure paths" in a tree where each path's gold coins add up to exactly your target amount.
Example: In a tree with nodes [10,5,-3,3,2,null,11,3,-2,null,1], if targetSum = 8, you might find paths like [5,3], [5,2,1], or even [-3,11] that all sum to 8.
Goal: Return the count of all valid downward paths that sum to targetSum.
Input & Output
Visualization
Time & Space Complexity
Visit each node exactly once during DFS traversal
HashMap can store up to O(n) prefix sums in worst case, plus O(h) recursion stack
Constraints
- The number of nodes in the tree is in the range [0, 1000]
- Each node value is in range [-109, 109]
- targetSum is in range [-1000, 1000]
- Path must go downward (parent to child direction only)