Given a binary tree, print it vertically. The following example illustrates the 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 a O(n2) solution in the previous post. In this post, an efficient solution based on the hash map is discussed. We need to check the Horizontal Distances from the root for all nodes. If two nodes have the same Horizontal Distance (HD), then they are on the same vertical line. The idea of HD is simple. HD for root is 0, a right edge (edge connecting to right subtree) is considered as +1 horizontal distance and a left edge is considered as -1 horizontal distance. For example, in the above tree, HD for Node 4 is at -2, HD for Node 2 is -1, HD for 5 and 6 is 0 and HD for node 7 is +2.
We can do preorder traversal of the given Binary Tree. While traversing the tree, we can recursively calculate HDs. We initially pass the horizontal distance as 0 for root. For left subtree, we pass the Horizontal Distance as Horizontal distance of root minus 1. For right subtree, we pass the Horizontal Distance as Horizontal Distance of root plus 1. For every HD value, we maintain a list of nodes in a hash map. Whenever we see a node in traversal, we go to the hash map entry and add the node to the hash map using HD as a key in a map.
Following is the C++ implementation of the above method. Thanks to Chirag for providing the below C++ implementation.
Time Complexity of hashing based solution can be considered as O(n) under the assumption that we have good hashing function that allows insertion and retrieval operations in O(1) time. In the above C++ implementation, map of STL is used. map in STL is typically implemented using a Self-Balancing Binary Search Tree where all operations take O(Logn) time. Therefore time complexity of the above implementation is O(nLogn).
Note that the above solution may not print nodes in same vertical order as they appear in tree. For example, the above program prints 12 before 9. See this for a sample run.
1 / \ 2 3 / / \ 4 5 6 7 / \ 8 10 9 \ 11 \ 12
Refer below post for level order traversal based solution. The below post makes sure that nodes of a vertical line are printed in the same order as they appear in the tree.
Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
Using Preorder Traversal Approach, Maintain the Order of Nodes in Same Vertical Order as They Appear in Tree:
We can also maintain the order of nodes in same vertical order as they appear in the tree. Nodes having same horizontal distance will print according to level order.
For example, In below diagram 9 and 12 have same horizontal distance. We can make sure that if a node like 12 comes below in same vertical line, it is printed after a node like 9
Idea: Instead of using horizontal distance as a key in the map, we will use horizontal distance + vertical distance as key. We know that the number of nodes can’t be more than integer range in a binary tree.
We will use first 30 bits of key for horizontal distance [MSB to LSB] and will use 30 next bits for vertical distance. Thus keys will be stored in map as per our requirement.
Below is the implementation of above approach.
Vertical order traversal :- 4 2 1 5 6 3 8 7 9
Time Complexity of the above implementation is O(n Log n).
Auxiliary Space: O(n)
Another Approach using computeIfAbsent method:
We can write the code in a more concise way, by using computeIfAbsent method of the map in java and by using a treemap for natural sorting based upon keys.
Below is the implementation of above approach.
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.
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Print a Binary Tree in Vertical Order | Set 1
- Find the kth node in vertical order traversal of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Vertical Sum in a given Binary Tree | Set 1
- Vertical width of Binary tree | Set 1
- Vertical width of Binary tree | Set 2
- Vertical Sum in Binary Tree | Set 2 (Space Optimized)
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Find maximum vertical sum in binary tree
- Find if given vertical level of binary tree is sorted or not
- Flatten binary tree in order of post-order traversal
- Vertical Order Traversal in Strings
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Print even positioned nodes of even levels in level order of the given binary tree