Vertical Order Traversal Problem Statement
Given a binary tree, return the vertical order traversal of the values of the nodes in the tree.
In a vertical order traversal, for each node at position (X, Y), (X-1, Y-1) is the left child, and (X+1, Y-1) is the right child.
Imagine running a vertical line from X = -∞ to X = +∞. Whenever this line encounters nodes, add the node values in the order from top to bottom, sorted by decreasing Y coordinates.
Note:
If two nodes share the same position, the leftmost node's value is added first.
Example:
Input:
The tree structure: Level order input is 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
2 7 5 1 6 3 11 4 9
Explanation:
Given the binary tree:The vertical order traversal is obtained.
Constraints:
1 ≤ T ≤ 100
0 ≤ N ≤ 3000
whereN
is the number of nodes0 ≤ VAL ≤ 10^5
whereVAL
is a node's valueTime Limit: 1 sec
Input:
An integer 'T' followed by T test cases. Each test case consists of a single line of space-separated values representing the level order traversal of the binary tree. Use -1 to denote null nodes.
Output:
For each test case, output the vertical order traversal, with node values separated by spaces. Each result should be on a new line.
Note:
Just implement the given function for the input. Printing is managed by the system.
Be the first one to answer
Add answer anonymously...
Top Blinkit Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
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