Generate Parentheses, traced one call at a time

Each box is one call to backtrack, labelled with the string built so far. Every call has two possible moves: add ( if fewer than n are used, or add ) if it closes an unmatched (. Red ✗ boxes are moves a rule refused, so a broken string is never built.

CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Recursion tree

Hover a string in res to find its call

not called yet running now paused, waiting on a child full length (saved) finished move refused by a rule

Call stack

Top frame runs; the rest are paused

Variables

path

balance mountain ( steps up, ) steps down. A ) is only legal above the floor.

res

Keyboard: ← → step, Space play or pause. The number of results is the Catalan number: 1, 2, 5, 14 for n = 1 to 4.