binary tree
5
3
3
1
2
9
queue (node, index)
front →
(empty)
← backBFS with position indices
▸1widthOfTree(root):2 queue = [(root, 0)]3 while queue not empty:4 base = queue.front.index // normalize5 width = queue.back.index - base + 16 maxWidth = max(maxWidth, width)7 for _ in range(len(queue)):8 node, i = queue.dequeue() // i already normalized9 enqueue (node.left, 2*i), (node.right, 2*i+1)10 return maxWidth
state
- ruleleft=2i right=2i+1
- maxWidth0