Path Sum III

Med
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 III

Approach

For each node, count paths starting there plus recurse into all descendants as new starts.

Complexity Analysis

Time
O(n^2) worst-case (simple DFS variant)
Space
O(h) recursion depth

Pattern

Root-Any-to-Any Path Sums

Why It Works

Every node can be a path start, and each call checks all downward continuations.

Updated Feb 2026