Path Sum III: Prefix Sum + Hash Map Counting Downward Paths (LeetCode 437) ACERS Guide

Subtitle / Summary The constraint “the path can start anywhere, but must go downward” makes root-to-leaf DP insufficient. This ACERS guide explains prefix sums on trees: convert any downward path into a difference of two prefix sums, maintain a frequency hash map during one DFS, and finish in O(n). Reading time: 12–15 min Tags: binary tree, prefix sum, DFS, hash map SEO keywords: Path Sum III, tree prefix sum, prefix-sum hash, LeetCode 437 Meta description: Count downward paths whose sum equals targetSum in O(n) via prefix sum + hash map, with derivation, tradeoffs, and multi-language implementations. A — Algorithm (Problem & Algorithm) Problem Restatement Given the root root of a binary tree and an integer targetSum, return the number of downward paths whose node values sum to targetSum. The path does not need to start at the root or end at a leaf, but it must go downward (parent → child). ...

February 4, 2026 · 15 min · map[name:Jeanphilo]