Path Sum
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 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
Explanation
There is a root-to-leaf path:
5 → 4 → 11 → 2
Its sum is:
5 + 4 + 11 + 2 = 22
Therefore, the answer is true.
Example 2
Input
root = [1,2,3] targetSum = 5
Output
false
Explanation
The root-to-leaf paths are:
1 → 2 = 3
1 → 3 = 4
No root-to-leaf path has a sum of 5.
Constraints
Hints:
Hint 1
Start from the root and keep track of the remaining sum needed for the current root-to-leaf path.
Hint 2
A path is valid only when you reach a leaf and the remaining target becomes equal to that leaf's value.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 19, 2026
Last updated
Aug 20, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output