Max Width of Binary Tree
Level-order visualizer with virtual index span tracking
Python Code
7 while queue:● level_size = len(queue)9 _, first = queue[0]; _, last = queue[-1]10 ans = max(ans, last - first + 1)11 for _ in range(level_size):12 node, idx = queue.popleft()13 idx -= first14 if node.left:15 queue.append((node.left, 2 * idx + 1))16 if node.right:17 queue.append((node.right, 2 * idx + 2))18 return ansCurrent Line (8): Initialize Level Span
Tree Structure
Operation:LEVEL
Active Frames
Stack is empty. Click Next to begin!
Step Explanation
Ready to Start
Click "Next Step" to begin max-width traversal. The flow is level-order BFS with virtual indices.
- > At each level, width = last_index - first_index + 1.
Unvisited
Left
Current
Right
Done