LESSON 03 OF 08Tree BFS · level order
0% learnt
BINARY TREE LEVEL-ORDER TRAVERSAL · BREADTH-FIRST SEARCH
The queue begins with [4]. Which node leaves first?
BFS always processes the oldest queued node. At the start, the root is the only node in the queue.
RULE TO APPLYA FIFO queue preserves increasing depth: dequeue one node, then enqueue its children.
LIVE ALGORITHM STATELevel size = 1, so this level contains only the root.
FIFO QUEUE · FRONT LEAVES FIRST
4
LESSON 031 / 2
REAL INTERVIEW PROBLEMRead a binary tree one level at a time.
Given a binary tree, return its values level by level from top to bottom and left to right.
INPUT[4, 2, 6, 1, 3, 5, 7]OUTPUT[[4], [2, 6], [1, 3, 5, 7]]
WHAT YOU WILL DORemove the node at the front of the queue, append its children, and close a level only after processing the number of nodes that were waiting when that level began.