Vertical Order Traversal Problem Statement
You are given a binary tree, and the task is to perform a vertical order traversal of the values of the nodes in the tree.
For a node at position ('X', 'Y'), the positions of its children are ('X'-1, 'Y'-1) for the left child and ('X'+1, 'Y'-1) for the right child.
A vertical line runs from X = -infinity to X = +infinity, adding node values it touches in order from top to bottom, based on decreasing 'Y' coordinates.
Input:
The first line contains an integer 'T', representing the number of test cases.
For each test case, a single line contains the level order traversal of the binary tree, with node values separated by a single space. Use -1 for null nodes.
Output:
For each test case, print the vertical order traversal of the binary tree, with node values separated by single spaces in a single line.
Example:
Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
4 2 1 5 3 7 6
Constraints:
- 1 <= 'T' <= 100
- 0 <= 'N' <= 3000
- 0 <= 'VAL' <= 105, where 'VAL' is a node's value
- Time Limit: 1 sec
Note:
If two nodes have the same position, the node added first appears on the left. Implement the function without handling the printing, as it is managed in the testing framework.
Be the first one to answer
Add answer anonymously...
Top Freshworks Lead Software Engineer interview questions & answers
Popular interview questions of Lead Software Engineer
Top HR questions asked in Freshworks Lead Software Engineer
>
Freshworks Lead Software Engineer 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