Top View of Binary Tree

Level-order visualizer: first node per horizontal distance

Read Here
Step0/22
Visited0
Result[]
Back To Trees List

Python Code

6 first = {}
7 while queue:
node, hd = queue.popleft()
9 if hd not in first:
10 first[hd] = node.data
11 if node.left:
12 queue.append((node.left, hd - 1))
13 if node.right:
14 queue.append((node.right, hd + 1))
15 return [first[key] for key in sorted(first)]
Current Line (8): Pop Queue Head

Tree Structure

123456
Operation:POP

Traversal Progress

Current Node

-

Phase

Pop Queue

Result Array

Traversal result appears here...
Step 1: POP

Processing Stack

Stack is empty. Click Next to begin!

Step Explanation

Ready to Start

Click "Next Step" to begin Top View traversal. The flow is level-order (BFS).

  • > For each horizontal distance, only the first visible node is captured.
Unvisited
Left
Current
Right
Done