Path Sum
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.
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.
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.
The root of a binary tree (it can be empty) and an integer targetSum.
true if some root-to-leaf path has the target sum, otherwise false.
The path 5 β 4 β 11 β 2 adds up to 22.
There are two root-to-leaf paths. 1 β 2 adds to 3, and 1 β 3 adds to 4. Neither is 5.
An empty tree has no paths at all.
- β’ The tree has between 0 and 5000 nodes.
- β’ β1000 β€ Node.val β€ 1000
- β’ β1000 β€ targetSum β€ 1000
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 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.
If I walk down and keep a running sum, what do I still need to know when I reach a node?
- 3.
Which node is allowed to decide the answer for a whole path?
- 4.
Can I stop early once the running sum is bigger than the target? Are all the values positive?
- 5.
What should the answer be for an empty tree?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Store the sum of every root-to-leaf path in a list, then look for the target | O(N) | O(N) |
| Optimal | Subtract each node's value on the way down; only a leaf checks for exactly 0 | O(N) | O(H) |
Why does this belong to this pattern?
- β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.
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.
Don't make this mistake
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.
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.
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.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.