# Reverse zigzag Traversal of a Binary Tree

Given a Binary Tree, the task is to print the Reverse zigzag Order of the tree.

Examples:

```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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C++

 `// C++ program to print reverse ` `// zigzag order of binary tree ` `#include ` `using` `namespace` `std; ` ` `  `// Binary tree node ` `struct` `node { ` `    ``struct` `node* left; ` `    ``struct` `node* right; ` `    ``int` `data; ` `}; ` ` `  `// Function to create a new ` `// Binary node ` `struct` `node* newNode(``int` `data) ` `{ ` `    ``struct` `node* temp = ``new` `node; ` ` `  `    ``temp->data = data; ` `    ``temp->left = NULL; ` `    ``temp->right = NULL; ` ` `  `    ``return` `temp; ` `} ` ` `  `// Recursive Function to find height ` `// of binary tree ` `int` `HeightOfTree(``struct` `node* root) ` `{ ` `    ``if` `(root == NULL) ` `        ``return` `0; ` ` `  `    ``// Compute the height of each subtree ` `    ``int` `lheight = HeightOfTree(root->left); ` `    ``int` `rheight = HeightOfTree(root->right); ` ` `  `    ``// Return the maximum of two ` `    ``return` `max(lheight + 1, rheight + 1); ` `} ` ` `  `// Function to Print nodes from right to left ` `void` `Print_Level_Right_To_Left(``struct` `node* root, ``int` `level) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` ` `  `    ``if` `(level == 1) ` `        ``cout << root->data << ``" "``; ` ` `  `    ``else` `if` `(level > 1) { ` `        ``Print_Level_Right_To_Left(root->right, level - 1); ` `        ``Print_Level_Right_To_Left(root->left, level - 1); ` `    ``} ` `} ` ` `  `// Function to Print nodes from left to right ` `void` `Print_Level_Left_To_Right(``struct` `node* root, ``int` `level) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` ` `  `    ``if` `(level == 1) ` `        ``cout << root->data << ``" "``; ` ` `  `    ``else` `if` `(level > 1) { ` `        ``Print_Level_Left_To_Right(root->left, level - 1); ` `        ``Print_Level_Left_To_Right(root->right, level - 1); ` `    ``} ` `} ` ` `  `// Function to print Reverse zigzag of ` `// a Binary tree ` `void` `PrintReverseZigZag(``struct` `node* root) ` `{ ` `    ``// Flag is used to mark the change ` `    ``// in level ` `    ``int` `flag = 1; ` ` `  `    ``// Height of tree ` `    ``int` `height = HeightOfTree(root); ` ` `  `    ``for` `(``int` `i = height; i >= 1; i--) { ` ` `  `        ``// If flag value is one print nodes ` `        ``// from right to left ` `        ``if` `(flag == 1) { ` `            ``Print_Level_Right_To_Left(root, i); ` ` `  `            ``// Mark flag as zero so that next time ` `            ``// tree is traversed from left to right ` `            ``flag = 0; ` `        ``} ` ` `  `        ``// If flag is zero print nodes ` `        ``// from left to right ` `        ``else` `if` `(flag == 0) { ` `            ``Print_Level_Left_To_Right(root, i); ` ` `  `            ``// Mark flag as one so that next time ` `            ``// nodes are printed from right to left ` `            ``flag = 1; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``struct` `node* root = newNode(5); ` `    ``root->left = newNode(9); ` `    ``root->right = newNode(3); ` `    ``root->left->left = newNode(6); ` `    ``root->right->right = newNode(4); ` `    ``root->left->left->left = newNode(8); ` `    ``root->left->left->right = newNode(7); ` ` `  `    ``PrintReverseZigZag(root); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to print reverse  ` `// zigzag order of binary tree  ` `class` `GfG ` `{ ` ` `  `// Binary tree node  ` `static` `class` `node  ` `{  ` `    ``node left;  ` `    ``node right;  ` `    ``int` `data;  ` `} ` ` `  `// Function to create a new  ` `// Binary node  ` `static` `node newNode(``int` `data)  ` `{  ` `    ``node temp = ``new` `node();  ` ` `  `    ``temp.data = data;  ` `    ``temp.left = ``null``;  ` `    ``temp.right = ``null``;  ` ` `  `    ``return` `temp;  ` `}  ` ` `  `// Recursive Function to find height  ` `// of binary tree  ` `static` `int` `HeightOfTree(node root)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return` `0``;  ` ` `  `    ``// Compute the height of each subtree  ` `    ``int` `lheight = HeightOfTree(root.left);  ` `    ``int` `rheight = HeightOfTree(root.right);  ` ` `  `    ``// Return the maximum of two  ` `    ``return` `Math.max(lheight + ``1``, rheight + ``1``);  ` `}  ` ` `  `// Function to Print nodes from right to left  ` `static` `void` `Print_Level_Right_To_Left(node root, ``int` `level)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return``;  ` ` `  `    ``if` `(level == ``1``)  ` `        ``System.out.print(root.data + ``" "``);  ` ` `  `    ``else` `if` `(level > ``1``)  ` `    ``{  ` `        ``Print_Level_Right_To_Left(root.right, level - ``1``);  ` `        ``Print_Level_Right_To_Left(root.left, level - ``1``);  ` `    ``}  ` `}  ` ` `  `// Function to Print nodes from left to right  ` `static` `void` `Print_Level_Left_To_Right(node root, ``int` `level)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return``;  ` ` `  `    ``if` `(level == ``1``)  ` `        ``System.out.print(root.data + ``" "``);  ` ` `  `    ``else` `if` `(level > ``1``) ` `    ``{  ` `        ``Print_Level_Left_To_Right(root.left, level - ``1``);  ` `        ``Print_Level_Left_To_Right(root.right, level - ``1``);  ` `    ``}  ` `}  ` ` `  `// Function to print Reverse zigzag of  ` `// a Binary tree  ` `static` `void` `PrintReverseZigZag(node root)  ` `{  ` `    ``// Flag is used to mark the change  ` `    ``// in level  ` `    ``int` `flag = ``1``;  ` ` `  `    ``// Height of tree  ` `    ``int` `height = HeightOfTree(root);  ` ` `  `    ``for` `(``int` `i = height; i >= ``1``; i--)  ` `    ``{  ` ` `  `        ``// If flag value is one print nodes  ` `        ``// from right to left  ` `        ``if` `(flag == ``1``)  ` `        ``{  ` `            ``Print_Level_Right_To_Left(root, i);  ` ` `  `            ``// Mark flag as zero so that next time  ` `            ``// tree is traversed from left to right  ` `            ``flag = ``0``;  ` `        ``}  ` ` `  `        ``// If flag is zero print nodes  ` `        ``// from left to right  ` `        ``else` `if` `(flag == ``0``)  ` `        ``{  ` `            ``Print_Level_Left_To_Right(root, i);  ` ` `  `            ``// Mark flag as one so that next time  ` `            ``// nodes are printed from right to left  ` `            ``flag = ``1``;  ` `        ``}  ` `    ``}  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``node root = newNode(``5``);  ` `    ``root.left = newNode(``9``);  ` `    ``root.right = newNode(``3``);  ` `    ``root.left.left = newNode(``6``);  ` `    ``root.right.right = newNode(``4``);  ` `    ``root.left.left.left = newNode(``8``);  ` `    ``root.left.left.right = newNode(``7``);  ` ` `  `    ``PrintReverseZigZag(root);  ` `} ` `}  ` ` `  `// This code is contributed by Prerna Saini. `

## Python3

 `# Python3 program to print reverse  ` `# zigzag order of binary tree  ` ` `  `# Binary tree node  ` `class` `Node:  ` `     `  `    ``def` `__init__(``self``, data): ` `        ``self``.data ``=` `data ` `        ``self``.left ``=` `None` `        ``self``.right ``=` `None` `     `  `# Recursive Function to find  ` `# height of binary tree  ` `def` `HeightOfTree(root):  ` ` `  `    ``if` `root ``=``=` `None``: ` `        ``return` `0` ` `  `    ``# Compute the height of each subtree  ` `    ``lheight ``=` `HeightOfTree(root.left)  ` `    ``rheight ``=` `HeightOfTree(root.right)  ` ` `  `    ``# Return the maximum of two  ` `    ``return` `max``(lheight ``+` `1``, rheight ``+` `1``)  ` ` `  `# Function to Print nodes from right to left  ` `def` `Print_Level_Right_To_Left(root, level):  ` ` `  `    ``if` `root ``=``=` `None``:  ` `        ``return` ` `  `    ``if` `level ``=``=` `1``: ` `        ``print``(root.data, end ``=` `" "``)  ` ` `  `    ``elif` `level > ``1``: ` `        ``Print_Level_Right_To_Left(root.right, ` `                                  ``level ``-` `1``)  ` `        ``Print_Level_Right_To_Left(root.left, ` `                                  ``level ``-` `1``)  ` ` `  `# Function to Print nodes from left to right  ` `def` `Print_Level_Left_To_Right(root, level):  ` ` `  `    ``if` `root ``=``=` `None``:  ` `        ``return` ` `  `    ``if` `level ``=``=` `1``:  ` `        ``print``(root.data, end ``=` `" "``)  ` ` `  `    ``elif` `level > ``1``:  ` `        ``Print_Level_Left_To_Right(root.left,     ` `                                  ``level ``-` `1``)  ` `        ``Print_Level_Left_To_Right(root.right, ` `                                  ``level ``-` `1``)  ` `     `  `# Function to print Reverse  ` `# zigzag of a Binary tree  ` `def` `PrintReverseZigZag(root):  ` ` `  `    ``# Flag is used to mark the  ` `    ``# change in level  ` `    ``flag ``=` `1` ` `  `    ``# Height of tree  ` `    ``height ``=` `HeightOfTree(root)  ` ` `  `    ``for` `i ``in` `range``(height, ``0``, ``-``1``):  ` ` `  `        ``# If flag value is one print  ` `        ``# nodes from right to left  ` `        ``if` `flag ``=``=` `1``:  ` `            ``Print_Level_Right_To_Left(root, i)  ` ` `  `            ``# Mark flag as zero so that next time  ` `            ``# tree is traversed from left to right  ` `            ``flag ``=` `0` `         `  `        ``# If flag is zero print nodes  ` `        ``# from left to right  ` `        ``elif` `flag ``=``=` `0``:  ` `            ``Print_Level_Left_To_Right(root, i)  ` ` `  `            ``# Mark flag as one so that next time  ` `            ``# nodes are printed from right to left  ` `            ``flag ``=` `1` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``root ``=` `Node(``5``)  ` `    ``root.left ``=` `Node(``9``)  ` `    ``root.right ``=` `Node(``3``)  ` `    ``root.left.left ``=` `Node(``6``)  ` `    ``root.right.right ``=` `Node(``4``)  ` `    ``root.left.left.left ``=` `Node(``8``)  ` `    ``root.left.left.right ``=` `Node(``7``)  ` ` `  `    ``PrintReverseZigZag(root)  ` ` `  `# This code is contributed by Rituraj Jain `

## C#

 `// C# program to print reverse  ` `// zigzag order of binary tree  ` `using` `System; ` ` `  `class` `GfG ` `{ ` ` `  `// Binary tree node  ` `public` `class` `node  ` `{  ` `    ``public` `node left;  ` `    ``public` `node right;  ` `    ``public` `int` `data;  ` `} ` ` `  `// Function to create a new  ` `// Binary node  ` `static` `node newNode(``int` `data)  ` `{  ` `    ``node temp = ``new` `node();  ` ` `  `    ``temp.data = data;  ` `    ``temp.left = ``null``;  ` `    ``temp.right = ``null``;  ` ` `  `    ``return` `temp;  ` `}  ` ` `  `// Recursive Function to find height  ` `// of binary tree  ` `static` `int` `HeightOfTree(node root)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return` `0;  ` ` `  `    ``// Compute the height of each subtree  ` `    ``int` `lheight = HeightOfTree(root.left);  ` `    ``int` `rheight = HeightOfTree(root.right);  ` ` `  `    ``// Return the maximum of two  ` `    ``return` `Math.Max(lheight + 1, rheight + 1);  ` `}  ` ` `  `// Function to Print nodes from right to left  ` `static` `void` `Print_Level_Right_To_Left(node root, ``int` `level)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return``;  ` ` `  `    ``if` `(level == 1)  ` `        ``Console.Write(root.data + ``" "``);  ` ` `  `    ``else` `if` `(level > 1)  ` `    ``{  ` `        ``Print_Level_Right_To_Left(root.right, level - 1);  ` `        ``Print_Level_Right_To_Left(root.left, level - 1);  ` `    ``}  ` `}  ` ` `  `// Function to Print nodes from left to right  ` `static` `void` `Print_Level_Left_To_Right(node root, ``int` `level)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return``;  ` ` `  `    ``if` `(level == 1)  ` `        ``Console.Write(root.data + ``" "``);  ` ` `  `    ``else` `if` `(level > 1) ` `    ``{  ` `        ``Print_Level_Left_To_Right(root.left, level - 1);  ` `        ``Print_Level_Left_To_Right(root.right, level - 1);  ` `    ``}  ` `}  ` ` `  `// Function to print Reverse zigzag of  ` `// a Binary tree  ` `static` `void` `PrintReverseZigZag(node root)  ` `{  ` `    ``// Flag is used to mark the change  ` `    ``// in level  ` `    ``int` `flag = 1;  ` ` `  `    ``// Height of tree  ` `    ``int` `height = HeightOfTree(root);  ` ` `  `    ``for` `(``int` `i = height; i >= 1; i--)  ` `    ``{  ` ` `  `        ``// If flag value is one print nodes  ` `        ``// from right to left  ` `        ``if` `(flag == 1)  ` `        ``{  ` `            ``Print_Level_Right_To_Left(root, i);  ` ` `  `            ``// Mark flag as zero so that next time  ` `            ``// tree is traversed from left to right  ` `            ``flag = 0;  ` `        ``}  ` ` `  `        ``// If flag is zero print nodes  ` `        ``// from left to right  ` `        ``else` `if` `(flag == 0)  ` `        ``{  ` `            ``Print_Level_Left_To_Right(root, i);  ` ` `  `            ``// Mark flag as one so that next time  ` `            ``// nodes are printed from right to left  ` `            ``flag = 1;  ` `        ``}  ` `    ``}  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main(String[] args)  ` `{  ` `    ``node root = newNode(5);  ` `    ``root.left = newNode(9);  ` `    ``root.right = newNode(3);  ` `    ``root.left.left = newNode(6);  ` `    ``root.right.right = newNode(4);  ` `    ``root.left.left.left = newNode(8);  ` `    ``root.left.left.right = newNode(7);  ` ` `  `    ``PrintReverseZigZag(root);  ` `} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:

```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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.