Asked inWalmart,SDE-2

Top View of a Binary Tree Problem Statement

You are given a Binary Tree of integers. Your task is to determine and return the top view of this binary tree.

The top view of a binary tree consists of all the nodes visible when the tree is viewed from the top.

Example:

Input:
For the given level order binary tree representation:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
10, 4, 2, 1, 3, 6
Explanation:

This is based on the visualization of the tree and identifying which node appears on the top from a perspective looking above the tree.

Constraints:

  • 1 ≤ T ≤ 100
  • 0 ≤ N ≤ 3000
  • 0 ≤ data ≤ 106 and data != -1

Where ‘T’ is the number of test cases, and ‘N’ is the total number of nodes in the binary tree, and “data” is the value of the binary tree node.

Time Limit: 1sec

Note:
The input format is a level order traversal of nodes where -1 represents a null node. For each test case, output the top view of the tree as a space-separated list of node values from left to right.
Be the first one to answer
Add answer anonymously...
Walmart SDE-2 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