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
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.
// min --> Minimum horizontal distance from root // max --> Maximum horizontal distance from root // hd --> Horizontal distance of current node from root findMinMax(tree, min, max, hd) if tree is NULL then return; if hd is less than min then *min = hd; else if hd is greater than max then *max = hd; findMinMax(tree->left, min, max, hd-1); findMinMax(tree->right, min, max, hd+1); printVerticalLine(tree, line_no, hd) if tree is NULL then return; if hd is equal to line_no, then print(tree->data); printVerticalLine(tree->left, line_no, hd-1); printVerticalLine(tree->right, line_no, hd+1);
Following is the implementation of above algorithm.
Vertical order traversal is 4 2 1 5 6 3 8 7 9
Time Complexity: Time complexity of above algorithm is O(w*n) where w is width of Binary Tree and n is number of nodes in Binary Tree. In worst case, the value of w can be O(n) (consider a complete tree for example) and time complexity can become O(n2).
This problem can be solved more efficiently using the technique discussed in this post. We will soon be discussing complete algorithm and implementation of more efficient method.
This article is contributed by Shalki Agarwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Find the kth node in vertical order traversal of a Binary Tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print Binary Tree levels in sorted order
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print characters and their frequencies in order of occurrence using Binary 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
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Vertical Sum in a given Binary Tree | Set 1
- Vertical width of Binary tree | Set 2