Given a binary tree, print it vertically. The following example illustrates vertical order traversal.
1 / \ 2 3 / \ / \ 4 5 6 7 \ \ 8 9 The output of print this tree vertically will be: 4 2 1 5 6 3 8 7 9
We have discussed an efficient approach in below post.
Print a Binary Tree in Vertical Order | Set 2 (Hashmap based Method)
The above solution uses preorder traversal and Hashmap to store nodes according to horizontal distances. Since the above approach uses preorder traversal, nodes in a vertical line may not be printed in the same order as they appear in the tree. For example, the above solution prints 12 before 9 in the below tree. See this for a sample run.
1 / \ 2 3 / \ / \ 4 5 6 7 \ / \ 8 10 9 \ 11 \ 12
If we use level order traversal, we can make sure that if a node like 12 comes below in the same vertical line, it is printed after a node like 9 which comes above in the vertical line.
1. To maintain a hash for the branch of each node. 2. Traverse the tree in level order fashion. 3. In level order traversal, maintain a queue which holds, node and its vertical branch. * pop from queue. * add this node's data in vector corresponding to its branch in the hash. * if this node hash left child, insert in the queue, left with branch - 1. * if this node hash right child, insert in the queue, right with branch + 1.
Vertical order traversal is 4 2 1 5 6 3 8 10 7 11 9 12
Time Complexity of the above implementation is O(n Log n). Note that the above implementation uses a map which is implemented using self-balancing BST.
We can reduce the time complexity to O(n) using unordered_map. To print nodes in the desired order, we can have 2 variables denoting min and max horizontal distance. We can simply iterate from min to max horizontal distance and get corresponding values from Map. So it is O(n)
Auxiliary Space: O(n)
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Level order traversal of Binary Tree using Morris Traversal
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Find the kth node in vertical order traversal of a Binary Tree
- Flatten Binary Tree in order of Level Order Traversal
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Density of Binary Tree using Level Order Traversal
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Print a Binary Tree in Vertical Order | Set 1
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Connect Nodes at same Level (Level Order Traversal)
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Insertion in n-ary tree in given order and Level order traversal
- Perfect Binary Tree Specific Level Order Traversal
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Check if the level order traversal of a Binary Tree results in a palindrome
- Specific Level Order Traversal of Binary Tree
- Boundary Level order traversal of a Binary Tree