2Brute2Best
🔥0

Path Sum II

Medium·LeetCode #113 Pro OnlyMedium
LeetCode
01

Problem Statement

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

Find every root-to-leaf path whose values add up to targetSum. A root-to-leaf path starts at the root and ends at a leaf (a node with no children). Return each matching path as a list of its node values, not as node references.

Input

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

Output

A list of every matching path. Each path is a list of values from the root down to a leaf.

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

5 + 4 + 11 + 2 = 22 and 5 + 8 + 4 + 5 = 22. The path 4 → 11 → 7 also adds to 22, but it does not start at the root, so it does not count.

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

The two paths add to 3 and 4. Neither is 5.

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

The only path, 1 → 2, adds to 3.

Constraints
  • • The tree has between 0 and 5000 nodes.
  • • −1000 ≤ Node.val ≤ 1000
  • • −1000 ≤ targetSum ≤ 1000
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“all root-to-leaf paths”
→ Visit every leaf with a full DFS. The answer is a list of paths, not a single yes or no.
“values add up to targetSum”
→ Carry the remaining amount down, as in Path Sum.
“return the paths”
→ The path values are state we must keep, not just one number.
03

Approach Complexity Ladder

Brute Force — copy the path into every callO(N · H)O(H²)

Pass the path by value, so every call copies the whole path built so far

Optimal — one shared path, copy only at answersO(N + A · H)O(H)

Push and pop one vector as we walk; copy it into the answers at matching leaves

Brute Force Intuition

Give each call its own vector of values. A call adds its node to the vector it was given, and passes a full copy of that vector to each child. When a leaf matches, its vector is an answer.

Recognition rule for this sub-pattern:

Carry information down the path (current sum, max so far, direction). Check it at leaves.
Remember this · Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

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.