DSA Tracker

Problem

Pattern this teaches:Tree DFS

Easy

Path Sum

An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Binary Trees
Sheets
2
Core for
4 roles

The problem

Start at the root of a binary tree and walk down to any leaf (a node with no children), adding up the values you pass. Return true if at least one such root-to-leaf walk adds up to exactly targetSum, and false if none does.

Example 1

Input
root=[6,3,8,2,null,7,9,null,5], targetSum=16
Output
true
Why
The path 6-3-2-5 adds to 16.

Example 2

Input
root=[4,2,5], targetSum=12
Output
false
Why
The two root-to-leaf paths sum to 6 and 9; neither is 12.

Example 3

Input
root=[7,5], targetSum=12
Output
true
Why
7 plus its only child 5 makes 12.

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-29

Instead 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

  1. 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.

  2. 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.

More Binary Trees problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .