Path Sum

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Path Sum

Approach

Subtract each node value while descending, and check target at leaf nodes.

Complexity Analysis

Time
O(n) where n is node count
Space
O(h) where h is tree height

Pattern

Root-to-Leaf DFS

Why It Works

A valid path is exactly a recursive reduction of remaining target by each visited value.

Updated Feb 2026