Step 1: Start BFS from root (1) at horizontal distance (hd) 0
Queue: [(1, hd=0)]
Top view map: {}Why: We begin from the root to explore all nodes level by level with their horizontal distances
Step 2: Dequeue (1, hd=0), add 1 to top view map at hd=0
Queue: []
Top view map: {0: 1}Why: First node at hd=0 is 1, so it is visible from top
Step 3: Enqueue left child (2, hd=-1) and right child (3, hd=1)
Queue: [(2, hd=-1), (3, hd=1)]
Top view map: {0: 1}Why: Children are added with updated horizontal distances
Step 4: Dequeue (2, hd=-1), add 2 to top view map at hd=-1
Queue: [(3, hd=1)]
Top view map: {-1: 2, 0: 1}Why: First node at hd=-1 is 2, so it is visible from top
Step 5: Enqueue right child (4, hd=0) of node 2
Queue: [(3, hd=1), (4, hd=0)]
Top view map: {-1: 2, 0: 1}Why: 4 is at hd=0 but 1 is already recorded, so 4 won't overwrite
Step 6: Dequeue (3, hd=1), add 3 to top view map at hd=1
Queue: [(4, hd=0)]
Top view map: {-1: 2, 0: 1, 1: 3}Why: First node at hd=1 is 3, so it is visible from top
Step 7: Enqueue right child (5, hd=2) of node 3
Queue: [(4, hd=0), (5, hd=2)]
Top view map: {-1: 2, 0: 1, 1: 3}Why: 5 is added with hd=2
Step 8: Dequeue (4, hd=0), hd=0 already has 1, so skip adding 4
Queue: [(5, hd=2)]
Top view map: {-1: 2, 0: 1, 1: 3}Why: Only first node at each hd is visible from top
Step 9: Dequeue (5, hd=2), add 5 to top view map at hd=2
Queue: []
Top view map: {-1: 2, 0: 1, 1: 3, 2: 5}Why: First node at hd=2 is 5, so it is visible from top
Result: Top view nodes in order of hd: 2 -> 1 -> 3 -> 5