Skip to main content
Back to problems
Leetcode
Easy
Trees
Recursion
Path Sum

Determine whether a binary tree contains a root-to-leaf path whose node values add up to a target sum.

Acceptance 0%
Problem Statement

Given the root of a binary tree and an integer targetSum, determine whether the tree has at least one root-to-leaf path such that the sum of the node values along that path equals targetSum.

A path must start at the root and end at a leaf. A leaf is a node with no children.

Input Format

  • A binary tree root root
  • An integer targetSum

Output Format

  • Return true if there exists at least one root-to-leaf path whose values sum to targetSum.
  • Otherwise, return false.

Constraints

  • The tree may be empty.
  • Node values and targetSum are integers.
  • The answer depends only on root-to-leaf paths, not any downward path.
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

true

Explanation

One valid path is 5 → 4 → 11 → 2, and its sum is 22.

Example 2

Input

root = [1,2,3], targetSum = 5

Output

false

Explanation

The root-to-leaf paths are 1 → 2 and 1 → 3, with sums 3 and 4.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.