2Brute2Best
πŸ”₯0

Path Sum

EasyΒ·LeetCode #112Basic Anchor
LeetCode
30-Second Mental ModelTree DFS β†’ Path state (top-down)
Complexity:O(N)β†’O(N)
Key Insight

Pass down the amount the rest of the path still has to reach: remaining = targetSum βˆ’ node.val at each node. At a leaf, the path works exactly when remaining is 0. The || between the two children means one matching path is enough, so the search stops early.

Core Validity Invariant

A root-to-leaf path is a match when its remainder is exactly 0 at the leaf. Only a leaf can finish a path, so a node with one child is not an end.

01

Problem

You are given the root of a binary tree and a number targetSum.

A root-to-leaf path starts at the root and goes down one child at a time until it reaches a leaf. A leaf is a node with no children. Return true if at least one such path adds up to targetSum. Otherwise return false.

Input

The root of a binary tree (it can be empty) and an integer targetSum.

Output

true if some root-to-leaf path has the target sum, otherwise false.

Example 1
Input: root = [5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1], targetSum = 22
Output: true

The path 5 β†’ 4 β†’ 11 β†’ 2 adds up to 22.

Example 2
Input: root = [1, 2, 3], targetSum = 5
Output: false

There are two root-to-leaf paths. 1 β†’ 2 adds to 3, and 1 β†’ 3 adds to 4. Neither is 5.

Example 3
Input: root = [], targetSum = 0
Output: false

An empty tree has no paths at all.

Constraints
  • β€’ The tree has between 0 and 5000 nodes.
  • β€’ βˆ’1000 ≀ Node.val ≀ 1000
  • β€’ βˆ’1000 ≀ targetSum ≀ 1000
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck β€” each hint is counted.

  1. 1.

    A path must start at the root and end at a leaf. What is different about a node that has only one child?

  2. 2.

    If I walk down and keep a running sum, what do I still need to know when I reach a node?

  3. 3.

    Which node is allowed to decide the answer for a whole path?

  4. 4.

    Can I stop early once the running sum is bigger than the target? Are all the values positive?

  5. 5.

    What should the answer be for an empty tree?

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceStore the sum of every root-to-leaf path in a list, then look for the targetO(N)O(N)
OptimalSubtract each node's value on the way down; only a leaf checks for exactly 0O(N)O(H)
09

Why does this belong to this pattern?

Clues in the problem statement
  • β€œroot-to-leaf path”The path starts at the root and ends at a leaf. So the check happens only at leaves, and the state flows top-down.
  • β€œadds up to targetSum”An exact sum. Instead of adding from the root at each leaf, carry what is still missing down the tree.
  • β€œreturn true or false”One matching path is enough, so the search can stop at the first success.
Top-down path+carry the remaining sum+check only at the leaf→Tree DFS → Path state (top-down)

The path has a fixed start (the root) and a fixed end type (a leaf). That is why one value carried down is enough: the remaining sum replaces the running total. This problem differs from its neighbours in the sub-pattern in one way: the answer is a yes or no, so the first match ends the search. Binary Tree Paths and Path Sum II need the whole path, not just its sum.

10

Don't make this mistake

Checking at a missing child instead of at a leaf
βœ— if (!root) return remaining == 0;
βœ“ if (!root) return false; if (!root->left && !root->right) return remaining == 0;

A missing child is not the end of a path. On [1, 2] with target 1: the right child of 1 is missing, and the remaining amount there is 1 βˆ’ 1 = 0, so the wrong version returns true. The only path is 1 β†’ 2, which adds to 3, so the answer is false. The same bug on an empty tree with target 0 also returns true.

Pattern confusion: stopping early like a positive-only sum
βœ— if (sum > targetSum) return false; // prune
βœ“ No early stop. Keep going until a leaf.

The sliding-window habit says: once the sum passes the target, it can only grow, so stop. That only works when every value is positive. Here values can be negative. On [3, -5, 2] with target -2: the path 3 β†’ -5 adds to -2, which is the target. Stopping at the root (3 is already above -2) returns false. The right answer is true.

Accepting a path that stops before a leaf
βœ— if (remaining == 0) return true; // checked at every node
βœ“ Check remaining == 0 only at a leaf.

A node with children is not the end of a path, even when the remaining amount is already 0. On [1, 2, 3] with target 1, the root alone adds to 1, so the wrong version returns true. The paths 1 β†’ 2 (sum 3) and 1 β†’ 3 (sum 4) both miss 1, so the answer is false.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.