Skip to content
Binary TreesMedium

23. Top View & Bottom View

Maps nodes to vertical column coordinates (HD) and retains the topmost or bottommost node in each column.

Time O(n) Space O(n)
Open in 3D Studio

Live loop — open the studio to scrub, add your own array, or race it in Battle Arena.

Asked in interviews at

GoogleAmazonNetflix

Reference implementation (Python)

def top_view(root):
    if not root: return []
    hd_map = {}
    queue = deque([(root, 0)])
    while queue:
        node, hd = queue.popleft()
        if hd not in hd_map:
            hd_map[hd] = node.val
        if node.left: queue.append((node.left, hd - 1))
        if node.right: queue.append((node.right, hd + 1))
    return [hd_map[k] for k in sorted(hd_map)]

Embed this visualization

Drop a live, looping 23. Top View & Bottom View canvas into a blog post or course page.

<iframe src="/embed/tree-top-bottom-view" width="100%" height="380" style="border:1px solid #e6e8ec;border-radius:14px" loading="lazy" title="3D algorithm visualization by CodeWithMunnaX"></iframe>

Frequently asked

What is the time and space complexity of 23. Top View & Bottom View?

23. Top View & Bottom View runs in O(n) time and O(n) auxiliary space.

Is the 23. Top View & Bottom View visualizer free?

The core 3D visualizer, Web Audio chimes and step intuition are free. A one-time ₹49 Pro Pass unlocks advanced algorithms, large custom arrays, Battle Arena and reel export.

Which companies ask 23. Top View & Bottom View in interviews?

23. Top View & Bottom View shows up in interviews at Google, Amazon, Netflix.

How does the 3D visualization of 23. Top View & Bottom View work?

Each array value becomes a 3D pillar whose height maps to its value. Comparisons glow amber, swaps flash red, and settled elements lock green while a floating pointer badge tracks the active index — synchronized line-by-line with the Python code.

More Binary Trees