Max Width of Binary Tree

Level-order visualizer with virtual index span tracking

Read Here
Step0/30
Levels0
Max0
Back To Trees List

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 -= first
14 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 ans
Current Line (8): Initialize Level Span

Tree Structure

123456
Operation:LEVEL

Traversal Progress

Current Node

-

Level Width

0

Level

-

Max Width

0

Width Per Level

Level widths appear here...
Phase: Start Level
Step 1: 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