Given a Binary Tree, the task is to print the Reverse zigzag Order of the tree.
Input: 1 / \ 2 3 / \ \ 4 5 6 Output: 6 5 4 2 3 1 Input: 5 / \ 9 3 / \ 6 4 / \ 8 7 Output: 7 8 6 4 3 9 5
Approach: The idea is to traverse the tree in a Reverse Level Order manner but with a slight modification. We will use a variable flag and initially set it’s value to one. As we complete the reverse level order traversal of the tree, from right to left we will set the value of flag to zero, so that next time we traverse the Tree from left to right and as we complete the traversal we set it’s value back to one. We will repeat this whole step until we have traversed the Binary Tree completely.
Below is the implementation of the above approach:
7 8 6 4 3 9 5
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.
- ZigZag Tree Traversal
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Level order traversal of Binary Tree using Morris Traversal
- Reverse Morris traversal using Threaded Binary Tree
- Reverse Clockwise spiral traversal of a binary tree
- Reverse Anti Clockwise Spiral Traversal of a Binary Tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Reverse Level Order Traversal
- Reverse Level Order traversal in spiral form
- Reverse an array in groups of given size | Set 3 (Single traversal)
- Cartesian tree from inorder traversal | Segment Tree
- If you are given two traversal sequences, can you construct the binary tree?
- Construct Special Binary Tree from given Inorder traversal
- Boundary Traversal of binary tree
- Perfect Binary Tree Specific Level Order Traversal
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
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.