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
anddata != -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...
Top Walmart SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app