Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Top View Of the Binary Tree (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a root of the binary tree, return an array containing nodes in its top view.

The top view of a binary tree consists of nodes that are visible when the tree is viewed from the top. If multiple nodes share the same horizontal distance from the root, only the first node encountered at that distance (in level order) is part of the top view.

Examples

Example 1

  • Input: root = [1, 2, 3, null, 5, 4, 6]
  • Expected Output: [2, 1, 3, 6]
  • Justification: It is a top view of the tree as shown in the diagram.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible