Top View of Binary Tree
Given a binary tree of integers, the task is to return the top view of the given binary tree. The top view of the binary tree is the set of nodes visible when viewed from the top.
Input:
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 format, with values separated by spaces. Use -1 to denote null nodes.
Output:
For each test case, return a list of integers representing the top view of the given tree.
Example:
For the given binary tree:
The top view of the tree will be {10, 4, 2, 1, 3, 6}.
Example Input:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Explanation:
Level 1: The root node is 1.
Level 2: Left child of 1 is 2, right child of 1 is 3.
Level 3: Left child of 2 is 4, right child is null (-1), left child of 3 is 5, right is 6.
Level 4: Right child of 4 is 7, all other children are null (-1).
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Constraints:
- 1 <= T <= 100
- 0 <= N <= 1000
- 0 <= data <= 10^6 and data != -1
Where ‘N’ is the total number of nodes, and 'data' is the value of the binary tree node.
Time limit: 1 sec
Note:
The input ends when all nodes at the last level are null (-1).
You don't need to print the output. Implement the function to return the answer.
Be the first one to answer
Add answer anonymously...
Top OodlesTechnologies Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in OodlesTechnologies Software Developer
>
OodlesTechnologies Software Developer Interview Questions
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