Return the top view of a binary tree from left to right

Quick Overview

Given a binary tree as a level-order list, return the values seen when looking down on the tree from above, ordered from left to right. The problem tests traversal order, tracking each node's horizontal distance from the root, and picking the right node when several share a column.

Return the top view of a binary tree from left to right

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a binary tree, return its top view. The top view of a binary tree is the set of nodes visible when the tree is viewed from the top. A node is included in the top view if it is the first node at its horizontal distance from the root, in level order. The horizontal distance of the root is 0, for a left child it is the parent's horizontal distance minus 1, and for a right child it is the parent's horizontal distance plus 1. Return the node values from left to right, that is, sorted by their horizontal distance. ### Function Signature ```python def top_view(tree: list[int | None]) -> list[int]: ``` ### Rules - `tree` uses the standard compact level-order encoding. The first entry is the root. After it, the entries come in pairs: the left child and then the right child of each real node, taking the real nodes in the order they appear in the list. A `None` entry has no children listed for it, and trailing `None` values may be left off. - Level order goes by depth from the root, and left to right within a depth. At each horizontal distance the view shows the highest node, and when two nodes at the same depth share a horizontal distance, the one further left is chosen. - Return one value per horizontal distance that occurs in the tree, ordered from the smallest distance to the largest. ### Constraints - `1 <= number of nodes <= 10^4` - `-1000 <= node value <= 1000` - `tree[0]` is not `None`. ### Examples ```text Input: tree = [1, 2, 3, 4, 5, 6, 7] Output: [4, 2, 1, 3, 7] ``` Nodes 5 and 6 both sit at horizontal distance 0 below the root, so 1 hides them. Every other distance has one node: 4 at -2, 2 at -1, 3 at 1 and 7 at 2. ```text Input: tree = [1, 2, 3, None, 4, None, None, None, 5, None, 6] Output: [2, 1, 3, 6] ``` The chain 4, 5, 6 runs down and to the right from node 2, so 4 lands at distance 0 under 1 and 5 at distance 1 under 3. Node 6 at distance 2 has nothing above it. ```text Input: tree = [1, 2, 3, None, 4, 5, None, None, 6, None, 7, None, 8, None, 9] Output: [2, 1, 3, 8] ``` Node 3 has no right child, so the only nodes at distance 2 are 8 and 9, both four levels below the root. They tie on depth, and 8 is chosen because it is reached through 2, the root's left child, so it comes first in level order.

Overview: Given a binary tree as a level-order list, return the values seen when looking down on the tree from above, ordered from left to right. The problem tests traversal order, tracking each node's horizontal distance from the root, and picking the right node when several share a column.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a binary tree, return its top view.

The top view of a binary tree is the set of nodes visible when the tree is viewed from the top. A node is included in the top view if it is the first node at its horizontal distance from the root, in level order. The horizontal distance of the root is 0, for a left child it is the parent's horizontal distance minus 1, and for a right child it is the parent's horizontal distance plus 1.

Return the node values from left to right, that is, sorted by their horizontal distance.

Function Signature

def top_view(tree: list[int | None]) -> list[int]:

Rules

  • tree uses the standard compact level-order encoding. The first entry is the root. After it, the entries come in pairs: the left child and then the right child of each real node, taking the real nodes in the order they appear in the list. A None entry has no children listed for it, and trailing None values may be left off.
  • Level order goes by depth from the root, and left to right within a depth. At each horizontal distance the view shows the highest node, and when two nodes at the same depth share a horizontal distance, the one further left is chosen.
  • Return one value per horizontal distance that occurs in the tree, ordered from the smallest distance to the largest.

Constraints

  • 1 <= number of nodes <= 10^4
  • -1000 <= node value <= 1000
  • tree[0] is not None .

Examples

Input: tree = [1, 2, 3, 4, 5, 6, 7]
Output: [4, 2, 1, 3, 7]

Nodes 5 and 6 both sit at horizontal distance 0 below the root, so 1 hides them. Every other distance has one node: 4 at -2, 2 at -1, 3 at 1 and 7 at 2.

Input: tree = [1, 2, 3, None, 4, None, None, None, 5, None, 6]
Output: [2, 1, 3, 6]

The chain 4, 5, 6 runs down and to the right from node 2, so 4 lands at distance 0 under 1 and 5 at distance 1 under 3. Node 6 at distance 2 has nothing above it.

Input: tree = [1, 2, 3, None, 4, 5, None, None, 6, None, 7, None, 8, None, 9]
Output: [2, 1, 3, 8]

Node 3 has no right child, so the only nodes at distance 2 are 8 and 9, both four levels below the root. They tie on depth, and 8 is chosen because it is reached through 2, the root's left child, so it comes first in level order.