Back to Practice
#0276
Path Sum
EasyDSA18 min10 XP
Problem
A tree node is [value, left, right]. Return True if any root-to-leaf path has values summing to target.
Why This Matters
Path Sum teaches how recursive state changes as you move down a tree.
Function Signature
def has_path_sum(root, target):
Examples
Example 1
Inputroot = [5,[4,None,None],[8,[13,None,None],[4,None,[1,None,None]]]], target = 18
OutputTrue
The path 5 -> 8 -> 4 -> 1 sums to 18.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Path exists
Inputtarget 18
ExpectedTrue
One root-to-leaf path hits the target.
No root-to-leaf path
Input[1, [2], [3]], target 5
ExpectedFalse
1 + 3 is 4, and 1 + 2 is 3.
Hidden Test Categories
negative valuessingle node equals targettarget only appears on non-leaf prefix