14. Path Sum
easyAsked at SalesforceDetermine if a binary tree has a root-to-leaf path summing to a target value. Salesforce uses this to test recursive path tracking, the foundation of their workflow-rule cascade evaluation.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Workflow-rule engine team uses path-evaluation as a warmup analogue.
- Blind (2025)— Common on Salesforce backend phone screens.
Problem
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.
Constraints
The number of nodes in the tree is in the range [0, 5000].-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000
Examples
Example 1
root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22trueExplanation: Path 5 -> 4 -> 11 -> 2 sums to 22.
Example 2
root = [1,2,3], targetSum = 5falseExample 3
root = [], targetSum = 0falseApproaches
1. Compute all paths then check
Build a list of all root-to-leaf sums, check if target is in it.
- Time
- O(n)
- Space
- O(h * leaves)
function hasPathSum(root, target) {
const sums = [];
function dfs(n, acc) {
if (!n) return;
acc += n.val;
if (!n.left && !n.right) sums.push(acc);
dfs(n.left, acc); dfs(n.right, acc);
}
dfs(root, 0);
return sums.includes(target);
}Tradeoff: Doesn't short-circuit. Salesforce will dock you for collecting all paths when only one is needed.
2. Recursive subtraction with short-circuit
Recurse subtracting node value from target. At a leaf, return target === node.val. Use || to short-circuit.
- Time
- O(n) worst, often less
- Space
- O(h)
function hasPathSum(root, targetSum) {
if (!root) return false;
if (!root.left && !root.right) return targetSum === root.val;
return hasPathSum(root.left, targetSum - root.val) ||
hasPathSum(root.right, targetSum - root.val);
}Tradeoff: Short-circuits on the first matching path via ||. The leaf check is the only point where target is actually compared.
Salesforce-specific tips
Salesforce workflow-rule engines evaluate cascading rules in tree form; they grade this on whether you handle the leaf-vs-internal distinction correctly. Bonus signal: mention that the same pattern extends to weighted-path evaluation (rule severity scoring), which is a real Salesforce feature.
Common mistakes
- Checking targetSum === 0 at null leaves — incorrectly returns true for non-leaf nulls (e.g., a node with only one child).
- Forgetting the empty-tree case — should be false even when targetSum is 0.
- Using the leaf check at internal nodes — false positives when a path 'passes through' the target sum but doesn't end at a leaf.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Path Sum II (LC 113) — return all such paths.
- Path Sum III (LC 437) — any path (not just root-to-leaf).
- Binary Tree Maximum Path Sum (LC 124).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why check at the leaf instead of at null children?
Checking at null lets non-leaf single-child nodes pass through and return true incorrectly. The problem specifies root-to-LEAF, so the check must be at leaves only.
Could I use BFS?
Yes, by pushing (node, runningSum) pairs into a queue. DFS is cleaner here because the recursion structure mirrors the path structure.
Practice these live with InterviewChamp.AI
Drill Path Sum and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →