Adobe Logo

Asked in Adobe

Top View of Binary Tree Problem Statement

Given a binary tree, your task is to print the Top View of the Binary Tree. The Top View is the set of nodes visible when the tree is viewed from the top. Please ensure to print the nodes from left to right order.

Example:

Input:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
2 35 2 10 2
Explanation:

This input represents the following binary tree:

alt text

The Top View includes nodes that are visible when looking down from the top, traversing the tree from the farthest left to the farthest right.

Input:

The input starts with:

t -> Number of test cases

Subsequent lines for each test case contain:

Binary tree nodes in level order, using -1 to indicate null nodes.

Output:

For each test case, print the nodes that form the Top View of the tree, from leftmost to rightmost.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 1000
  • Time Limit: 1 second
Note

The given input format describes each binary tree in a singular line with space-separated integer values, which include -1 for any null child nodes.

AnswerBot
4mo

The task is to print the Top View of a Binary Tree, which is the set of nodes visible when viewed from the top, in left to right order.

  • Traverse the binary tree in level order and maintain a map to sto...read more

Help your peers!
Select
Add answer anonymously...

Top Mts1 Interview Questions Asked at Adobe

Q. Rearrange The Array Problem Statement You are given an array/list 'NUM' of integ...read more
Q. Level Order Traversal of Binary Tree Given a binary tree of integers, return its...read more
Q. Maximum Sum Problem Statement You are given an array ARR of N integers. Your tas...read more
Mts1 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