Back to problems

Path Sum in a Binary Tree

Algorithm · Salesforce · Easy

You are given the root node of a binary tree and an integer targetSum. Determine if there exists a path that starts at the root and ends at any leaf such that the sum of the values along the nodes on that path equals targetSum. A leaf node is defined as a node that has neither a left child nor a right child. Implement a function hasPathSum(root, targetSum) that returns true when such a root‑to‑leaf path is present, and false otherwise. Example 1: Explanation: The path 10 → 5…

Checking your access…