# Print the Binary Tree in Vertical Order Path.

Objective: Given a binary tree, print it in vertical order path.

What is Vertical Order

Vertical-Order-Print-Example

as you can see in the example above, [4],[2], [1,5,6],[3],[7] are the verical order of the given binary tree.

Approach:

• Its a tricky solution.
• Do the inorder traversal.
• Take a variable called level, when ever you go left, do level++ AND when ever you go right do level–.
• With step above we have separated out the levels vertically.
• Now you need to store the elements of each level, so create a TreeMap and the (key,value) pair will be (level,elements at that level).
• At the end iterate through the TreeMap and print the results.

Vertical-Order-Sum-Implementation

Complete Code:

Output:

```[4]
[2]
[5, 1, 6]
[3]
[7]
```

__________________________________________________
Top Companies Interview Questions..-

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

#### You may also like...

• user_2005

did not understand :
Node y = vertical(root.left, –level);

• tutorialhorizon

sorry for the late reply. as it is explained in the article that when ever we go to left or right we need to keep track of the level we are in

• Ankita

Its printing [5,1,6] not [6,1,5]

• tutorialhorizon

Thanks ankita, though the program is still correct, I have corrected the output.

• shaiksuraz

We use TreeMap here,to store natural ordering of elements. I think same is the case with std::Map in C++. Am i right?