Given a Binary Tree, the task is to print the elements in the Vertical Zig-Zag traversal order.
Vertical Zig-Zag traversal of a tree is defined as:
- Print the elements of the first level in the order from right to left, if there are no elements left then skip to the next level.
- Print the elements of the last level in the order from left to right, if there are no elements left then skip to the previous level.
- Repeat the above steps while there are nodes left to traverse.
Input: 1 / \ 2 3 \ / \ 4 5 6 / \ 7 8 Output: 1, 7, 3, 8, 2, 4, 6, 5 Explanation: 1. First print elements of 1st level which will be printed as follows: 1 2. Now remaining part of the tree is * / \ 2 3 \ / \ 4 5 6 / \ 7 8 3. Now move to 4th level print from leftmost one element, which will be: 7 4. Now tree becomes: * / \ 2 3 \ / \ 4 5 6 / \ * 8 5. Now move to since we move from 2nd level since we move from the lower level to higher-level so start from rightmost, so the element will be: 3 6. Now tree becomes: * / \ 2 * \ / \ 4 5 6 / \ * 8 7. Now again move to 4th level print from the leftmost remaining element, which will be 8 8. Now tree becomes: * / \ 2 * \ / \ 4 5 6 / \ * * 9. Now again move to 2nd level print from the rightmost remaining element, which will be 2 continue this way until all elements are not traversed
Approach: Create a vector tree where tree[i] will store all the nodes of the tree at level i. Take two pointers p1 pointing to the first level and p2 pointing to the last level. Now, start printing the nodes in an alternate fashion using these two pointers (i.e. right to left for p1 and left to right for p2). If there are no nodes left in the level pointed by p1 then move to the next level and if there are no nodes left in the level pointed by p2 then move to the previous level.
Below is the implementation of the above approach:
1 7 3 8 2 4 6 5
Time Complexity: O(n)
- ZigZag Tree Traversal
- Reverse zigzag Traversal of a Binary Tree
- Find the kth node in vertical order traversal of a Binary Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Vertical Sum in a given Binary Tree | Set 1
- Vertical width of Binary tree | Set 2
- Vertical width of Binary tree | Set 1
- Print a Binary Tree in Vertical Order | Set 1
- Find maximum vertical sum in binary tree
- Vertical Sum in Binary Tree | Set 2 (Space Optimized)
- Find if given vertical level of binary tree is sorted or not
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Cartesian tree from inorder traversal | Segment Tree
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.