AlgoMaster Logo

Path Sum III

Last Updated: April 1, 2026

medium
4 min read

Understanding the Problem

This problem is a twist on the classic Path Sum problems. In Path Sum I and II, paths had to start at the root and end at a leaf. Here, the rules are much looser: a valid path can start at any node and end at any node below it. The only constraint is that the path must go downward, following parent-to-child edges.

This changes things significantly. Consider a tree where nodes along a root-to-leaf route have values [10, 5, 3, -2]. The subpath [5, 3] sums to 8, and it starts in the middle of the tree. We need to count paths like that.

The key observation is that any valid path is a contiguous subpath of some root-to-node path. If we think of the values along a root-to-node route as an array, we're essentially asking: how many contiguous subarrays sum to the target? That reframing connects this tree problem to the well-known "subarray sum equals K" problem, and that's where the prefix sum technique comes in.

Key Constraints:

  • Number of nodes up to 1000 → This is a small tree. Even O(n^2) approaches will pass easily. But learning the O(n) technique is valuable for interviews and larger inputs.
  • -10^9 <= Node.val <= 10^9 → Node values can be very large (positive or negative). We need to use long in languages like Java/C++ to avoid integer overflow when computing prefix sums.
  • -1000 <= targetSum <= 1000 → The target can be negative, so we can't make assumptions about path sums being positive.

Approach 1: Brute Force (DFS from Every Node)

Intuition

The most straightforward idea: treat every node as a potential starting point for a path, then walk downward from that node and count how many paths hit the target sum. Since valid paths must go downward, from any starting node we just do a DFS and keep a running sum. Whenever the running sum equals targetSum, we've found a valid path.

We need two layers of recursion here. The outer layer visits every node in the tree (to try it as a starting point). The inner layer, starting from a given node, explores all downward paths and counts the ones that sum to the target.

Algorithm

  1. Traverse the entire tree using DFS. At each node, call a helper function that counts paths starting from that node.
  2. In the helper function, maintain a running sum. Add the current node's value to it.
  3. If the running sum equals targetSum, increment the count.
  4. Recurse into the left and right children, carrying the running sum forward.
  5. The total count is the sum of valid paths found from every starting node.

Example Walkthrough

1Start outer DFS at root (10). Inner DFS: try paths starting from 10
10start533-221-311
1/7

Code

This works, but for every node we re-traverse large portions of the tree. What if we could compute running sums from the root just once and use a data structure to instantly check whether any earlier prefix sum, when subtracted, gives us the target?

Approach 2: Prefix Sum with Hash Map (Optimal)

Intuition

Here's the key insight that transforms this from a tree problem into something we've seen before. Consider any path from the root to the current node. The values along that path form an array. A valid path that ends at the current node is just a contiguous subarray of that root-to-node array that sums to targetSum.

Now, how do we efficiently find contiguous subarrays with a given sum? This is the classic "subarray sum equals K" technique. If the prefix sum from the root to the current node is currentSum, and at some ancestor the prefix sum was currentSum - targetSum, then the path between that ancestor and the current node sums to exactly targetSum.

So we maintain a hash map that counts how many times each prefix sum has appeared on the current root-to-node path. At each node, we check how many ancestors had a prefix sum of currentSum - targetSum. That count tells us exactly how many valid paths end at this node.

The tricky part is backtracking. When we finish processing a subtree and move to a sibling branch, the prefix sums from the finished subtree shouldn't be in our map anymore. So after exploring both children of a node, we decrement that node's prefix sum count in the map. This keeps the map accurate for only the current root-to-node path.

Algorithm

  1. Initialize a hash map with {0: 1}. This handles the case where a path from the root itself sums to the target.
  2. DFS through the tree, maintaining a running currentSum from the root.
  3. At each node, add node.val to currentSum.
  4. Check the map for currentSum - targetSum. The count stored there is the number of valid paths ending at this node.
  5. Add currentSum to the map (increment its count).
  6. Recurse into the left and right children.
  7. After both children are processed, remove currentSum from the map (decrement its count). This is the backtracking step.

Example Walkthrough

1Visit 10: prefix=10, check 10-8=2 in map → not found
10prefix=10533-221-311
1/8
1Add prefix 10 → map. Need 2 for match (not found)
0
:
1
10
:
1
1/8

Code