Path Sum (LeetCode 112)
Determine if the tree has a root-to-leaf path summing to a target.
Examples
The path 5 -> 4 -> 11 -> 2 sums to 22.
Paths are 1->2 (3) and 1->3 (4). No path sums to 5.
Problem Understanding
Return true when some path that starts at root and ends at a leaf has node values adding up to exactly targetSum. The path must run all the way down: stopping at an inner node does not count, which is what makes this a root-to-leaf check.
A leaf is a node with no children.
Algorithm Strategy (Top-Down State)
We use Pre-order DFS (Top-Down) logic.
Instead of calculating the sum from scratch at every leaf, we subtract the current node's value from the targetSum as we go down.
- Consume Value:
remaining = target - node.val. - Check Leaf: If
nodeis a leaf (no left, no right) ANDremaining == 0, we found a path! Returntrue. - Recurse: Check
dfs(node.left, remaining)ORdfs(node.right, remaining). If either returnstrue, propagate it up. - Base Case: If
nodeisnull, returnfalse.
Interactive Visualization
TARGET 22 · A PATH MUST END AT A LEAF
CALL
Still needed
:
Answer
:
call stack
hasPathSum(5, 22)
Next
targetSum -= 5: 22 - 5 = 17 still needed below
Tree
target
Each node subtracts its value from what is still needed, and the remainder appears under it. A leaf that reaches exactly 0 turns green. Once a path is found, || stops, so the right side of that node is never called.
Dry Run: Target = 22
| Node | Current Target | Value to Subtract | Remaining Needed | Action |
|---|---|---|---|---|
| 5 (Root) | 22 | 5 | 17 | Recurse Left (Target 17) |
| 4 | 17 | 4 | 13 | Recurse Left (Target 13) |
| 11 | 13 | 11 | 2 | Recurse Right (Target 2) |
| 2 (Leaf) | 2 | 2 | 0 | Success! Return True |
Edge Cases & Common Mistakes
- Null Root: Return
false(no path exists). - Negative Values: The logic works fine with negatives (
remainingcan increase). - Path stops before leaf: e.g. path matches sum but node has children. This is NOT a root-to-leaf path. Must strictly check
!left && !right.
Solution
Recursive check for path existence.
Path Sum Code
1var hasPathSum = function(root, targetSum) {2 if (!root) return false;3 targetSum -= root.val;4 if (!root.left && !root.right) {5 return targetSum === 0;6 }7 return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);8};
Path Sum in Python, Java, C++, TypeScript and C
Path Sum in Python
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root:
return False
targetSum -= root.val
if not root.left and not root.right:
return targetSum == 0
return self.hasPathSum(root.left, targetSum) or self.hasPathSum(root.right, targetSum)Path Sum in Java
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
targetSum -= root.val;
if (root.left == null && root.right == null) {
return targetSum == 0;
}
return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
}
}Path Sum in C++
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if (!root) return false;
targetSum -= root->val;
if (!root->left && !root->right) {
return targetSum == 0;
}
return hasPathSum(root->left, targetSum) || hasPathSum(root->right, targetSum);
}
};Path Sum in TypeScript
function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
if (!root) return false;
targetSum -= root.val;
if (!root.left && !root.right) {
return targetSum === 0;
}
return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
}Path Sum in C
bool hasPathSum(struct TreeNode* root, int targetSum) {
if (root == NULL) return false;
targetSum -= root->val;
if (root->left == NULL && root->right == NULL) {
return targetSum == 0;
}
return hasPathSum(root->left, targetSum) || hasPathSum(root->right, targetSum);
}More practice: DSA interview questions with answers · patterns cheat sheet
