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.

AnswerBot
4mo
Vertical order traversal of binary tree is obtained by running a vertical line from -∞ to +∞ and adding node values in top to bottom order, sorted by decreasing Y coordinates.
Implement a function to p...read more
Help your peers!
Add answer anonymously...
Blinkit Software Developer Intern interview questions & answers
A Software Developer Intern was asked Q. Top View of Binary Tree Given a binary tree of integers, the task is to return t...read more
A Software Developer Intern was asked Q. Maximum Possible Time Problem Statement Given an array ARR consisting of exactly...read more
A Software Developer Intern was asked Q. Sum of LCM Problem Statement Given an integer 'N', calculate and print the sum o...read more
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked Q1. Maximum Possible Time Problem Statement Given an array ARR consisting of exactly...read more
A Software Developer Intern was asked Q2. Sum of LCM Problem Statement Given an integer 'N', calculate and print the sum o...read more
A Software Developer Intern was asked Q3. Vertical Order Traversal Problem Statement Given a binary tree, return the verti...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

