workshop private

← all creations

Rebase

viz · created 2026-09-18

The LC 662 width walk drawn on the complete tree it pretends the input is — every node at the seat 2i / 2i+1 assigns it, the empty seats between a level's ends ghosted in and counted, and the same walk run under four arithmetics, so a left-leaning chain shows a JS double quietly seating two children in one chair past 2^53, a Java int wrapping negative and still getting the width right, and the per-level rebase that makes the whole question go away.

algorithmsinterview-prepcanvas

Maximum Width of Binary Tree (LC 662) asks for the widest level of a tree, counting the null positions a complete tree would have between the leftmost and rightmost real nodes. The idea that makes it linear is a seat number: root at 1, children of seat i at 2i and 2i+1, so a level’s width is just right − left + 1 and the nulls are counted without ever existing. Everyone gets that far in a minute. The problem’s actual content is what those seats do on a deep tree — a seat at depth d is a d-bit number, the constraints promise the answer fits in 32 bits and say nothing about the seats, and a 3000-node tree that leans all the way left puts a 3000-bit seat on its last node.

So the piece runs one BFS, level by level, and draws the ghosts — dashed seats between consecutive real nodes, or a dashed run labelled 4,094 empty when there are too many to draw — with the width bracket under the current level written out as the subtraction it is. Then it runs the same walk under four arithmetics, switchable mid-walk on the same level:

Presets: the three LC examples, the left chain with a fork at the bottom (depth adjustable to 80), two spines (two nodes on the bottom level, 2^d − 2 ghosts between them — the width is the ghosts), and random trees with a lean slider. Space plays and pauses, the arrows step, 14 switch the arithmetic in place, r restarts.

Reuse

src/rebase.js is a framework-free ES module:

No rendering or timers in the module; the canvas demo is reference code.

Gotchas