
Top View of Binary Tree Problem Statement
Given a Binary Tree of integers, you are tasked with finding the top view of the binary tree. The top view is the set of nodes visible when the tree is viewed from the top.
Input:
The input consists of multiple test cases.
The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains elements of the tree in level order form; node values are separated by a single space. A node is represented as -1 if it is null.
Output:
For each test case, return a list or vector containing the elements of the top view of the given tree.
Example:
Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
{10, 4, 2, 1, 3, 6}
Constraints:
1 ≤ T ≤ 100
0 ≤ N ≤ 1000
0 ≤ data ≤ 106
anddata ≠ -1
Where 'N' is the total number of nodes in the binary tree, and 'data' is the node value.
Time limit: 1 sec
Note:
- The first not-null node of the previous level is considered the parent of the first two nodes of the current level, the second not-null node is the parent for the next two nodes, and so on.
- The input ends when all nodes at the last level are null (-1).
No need to print the output, focus on implementing the function to solve the problem.

AnswerBot
11d

Find the top view of a Binary Tree by returning a list of visible nodes when viewed from the top.
Traverse the Binary Tree in level order and keep track of the horizontal distance of each node from the...read more

Help your peers!
Add answer anonymously...
Top Oyo Rooms Software Developer interview questions & answers
Popular interview questions of Software Developer
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