Path Sum
An easy Binary Trees problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Binary Trees
- Sheets
- 2
- Core for
- 4 roles
- Platform
- LeetCode
The problem
Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children.
Example 1
- Input
- root=[5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum=22
- Output
- true
- Why
- The path 5->4->11->2 sums to 22.
Example 2
- Input
- root=[1,2,3], targetSum=5
- Output
- false
- Why
- No root-to-leaf path sums to 5.
Example 3
- Input
- root=[1,2], targetSum=3
- Output
- true
- Why
- The path 1->2 sums to 3.
Constraints
- The number of nodes is in the range [0, 5000].
- -1000 <= Node.val <= 1000
- -1000 <= targetSum <= 1000
How to think about it
Updated 2026-09-09Instead of accumulating a running sum downward and checking against targetSum, subtract each node's value from targetSum as you descend. A valid path exists if you land on a leaf node whose value equals the remaining balance.
Approaches, worst first
Iterative paired stack DFS
time O(n) · space O(h)
Push tuples of `(node, remainingSum)` to a stack. Pop, check if leaf and `remainingSum === 0`. If not, push existing children with their subtracted remainders. Manages memory explicitly without recursion frames.
Recursive subtraction DFSWrite this one
time O(n) · space O(h)
If root is null, return false. Subtract `root.val` from `targetSum`. If current node is a leaf, check if `targetSum === 0`. Otherwise return `hasPathSum(left, rem) || hasPathSum(right, rem)`. Clean, expressive, and terminates on first match.
Where people lose marks · 3
- Returning true on a non-leaf node whose path sum matches targetSum; the problem strictly requires paths to terminate at a leaf where `left === null && right === null`.
- Pruning branches assuming positive values: node values can be negative, so a running sum can overshoot and decrease later.
- Checking `root === null && targetSum === 0` as a success condition incorrectly accepts empty trees or single-child parents whose missing child passes 0 balance.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
What each operation costs
| Operation | Time |
|---|---|
| traverse all nodes using recursion or queue | O(n) |
| search for an arbitrary value in an unordered tree | O(n) |
| call stack memory on balanced tree | O(log n) |
| call stack memory on skewed tree | O(n) |
What usually goes wrong with Binary Trees
- Dereferencing left or right child pointers without checking if the current node is null, throwing null pointer errors on empty trees or leaf nodes.
- Defining a leaf node incorrectly by stopping when either child is null instead of checking that both left and right children are simultaneously null.
- Computing tree diameter by taking left height plus right height inside a recursive helper without updating a global maximum across every visited node.
Which roles need this problem
Binary Trees is a core topic for these 4 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 5 more roles, including Full-Stack Developer, Android Developer, iOS Developer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Binary Trees problems
Problem set and role mapping as of .