Asked inAdobe,Mts1

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.

Be the first one to answer
Add answer anonymously...
Adobe Mts1 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter