Top View of Binary Tree Problem Statement
Given a binary tree, your task is to print the Top View of the Binary Tree. The Top View is the set of nodes visible when the tree is viewed from the top. Please ensure to print the nodes from left to right order.
Example:
Input:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
2 35 2 10 2
Explanation:
This input represents the following binary tree:
The Top View includes nodes that are visible when looking down from the top, traversing the tree from the farthest left to the farthest right.
Input:
The input starts with:
t -> Number of test cases
Subsequent lines for each test case contain:
Binary tree nodes in level order, using -1 to indicate null nodes.
Output:
For each test case, print the nodes that form the Top View of the tree, from leftmost to rightmost.
Constraints:
1 <= T <= 100
1 <= N <= 1000
- Time Limit: 1 second
Note
The given input format describes each binary tree in a singular line with space-separated integer values, which include -1
for any null child nodes.
Top Adobe Mts1 interview questions & answers
Popular interview questions of Mts1
Reviews
Interviews
Salaries
Users/Month