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.
- Reverse Clockwise spiral traversal of a binary tree
- Reverse Morris traversal using Threaded Binary Tree
- Reverse Anti Clockwise Spiral Traversal of a Binary Tree
- ZigZag Tree Traversal
- Level order traversal of Binary Tree using Morris Traversal
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Zig-Zag traversal of a Binary Tree using Recursion
- Diagonal Traversal of Binary Tree
- Mix Order Traversal of a Binary Tree
- Density of Binary Tree in One Traversal
- Boundary Traversal of binary tree
- Sideways traversal of a Complete Binary Tree
- Triple Order Traversal of a Binary Tree
- Left-Right traversal of all the levels of Binary tree
- Clockwise Spiral Traversal of Binary Tree
- Middle To Up-Down Order traversal of a Binary Tree
- If you are given two traversal sequences, can you construct the binary tree?
- Flatten Binary Tree in order of Zig Zag traversal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.