Path Sum Problem
Path Sum Problem — ExecCode Medium DSA Practice
Solve the Path Sum problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.
Problem description
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.
Examples
Input root = [5, 4, 8, 13, null, 2, 4, 1, null, null, null, null, 5], targetSum = 22; Output true. Input root = [1, 2, 3], targetSum = 5; Output false. Input root = [1, 2], targetSum = 3; Output true
Constraints
The number of nodes in the tree is in the range [0, 5000]. -1000 <= Node.val <= 1000 -1000 <= targetSum <= 1000
Practice Path Sum free on ExecCode. Browse DSA problems, topic map, and placement guides.