Top View of Binary Tree Problem Statement

Given a Binary Tree of integers, you are tasked with finding the top view of the binary tree. The top view is the set of nodes visible when the tree is viewed from the top.

Input:

The input consists of multiple test cases.

The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains elements of the tree in level order form; node values are separated by a single space. A node is represented as -1 if it is null.

Output:

For each test case, return a list or vector containing the elements of the top view of the given tree.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
{10, 4, 2, 1, 3, 6}

Constraints:

  • 1 ≤ T ≤ 100
  • 0 ≤ N ≤ 1000
  • 0 ≤ data ≤ 106 and data ≠ -1

Where 'N' is the total number of nodes in the binary tree, and 'data' is the node value.

Time limit: 1 sec

Note:

  • The first not-null node of the previous level is considered the parent of the first two nodes of the current level, the second not-null node is the parent for the next two nodes, and so on.
  • The input ends when all nodes at the last level are null (-1).

No need to print the output, focus on implementing the function to solve the problem.

AnswerBot
4mo

Find the top view of a Binary Tree by returning a list of visible nodes when viewed from the top.

  • Traverse the Binary Tree in level order and keep track of the horizontal distance of each node from the...read more

Help your peers!
Select
Add answer anonymously...

Oyo Rooms Software Developer interview questions & answers

A Software Developer was asked Q. Given the head of a singly linked list and two integers m and n, reverse the nod...read more
A Software Developer was asked Q. Merge sort algorithm and divide array in the chunks
A Software Developer was asked Q. Position of Right Most Set Bit Determine the position of the rightmost set bit i...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Given the head of a singly linked list and two integers m and n, reverse the nod...read more
A Software Developer was asked Q2. Merge sort algorithm and divide array in the chunks
A Software Developer was asked Q3. Position of Right Most Set Bit Determine the position of the rightmost set bit i...read more
Oyo Rooms Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits