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.
AnswerBot
4mo

The task is to determine and return the top view of a given Binary Tree of integers.

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

  • Identify the no...read more

Help your peers!
Select
Add answer anonymously...

Walmart SDE-2 interview questions & answers

A SDE-2 was asked Q. Bipartite Graph Problem Statement Determine if a given graph is bipartite. A gra...read more
A SDE-2 was asked Q. Can you explain the DHCP Protocol?
A SDE-2 was asked Q. Can you explain the TCP/IP protocol?

Popular interview questions of SDE-2

A SDE-2 was asked Q1. Bipartite Graph Problem Statement Determine if a given graph is bipartite. A gra...read more
A SDE-2 was asked Q2. Can you explain the DHCP Protocol?
A SDE-2 was asked Q3. Can you explain the TCP/IP protocol?
Walmart SDE-2 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