May I know the logic and code for building the vertical order tree and how to take the input for it?
Vertical order print
Hey @nikitaguptanikki2
The Input is taken in Level order , just like you print tree in level order.
1 is the root node , then 2 and 3 are its children and the children of 2 i.e 4 and 5 and so on.
So the tree will be
1
2 3
4 5 6 7
@nikitaguptanikki2
Regarding the approach:
The idea is to traverse the tree once and get the minimum and maximum horizontal distance with respect to root. For the tree shown above, minimum distance is -2 (for node with value 4) and maximum distance is 3 (For node with value 9).
Once we have maximum and minimum distances from root, we iterate for each vertical line at distance minimum to maximum from root, and for each vertical line traverse the tree and print the nodes which lie on that vertical line.
Also, you need to store the elements of each distance, so create a map and the (key,value) pair will be (distance,elements at that distance).
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.