# Anti Clockwise spiral traversal of a binary tree

Given a binary tree, the task is to print the nodes of the tree in an anti-clockwise spiral manner.

Examples:

```Input:
1
/  \
2    3
/ \  / \
4   5 6  7
Output: 1 4 5 6 7 3 2

Input:
1
/  \
2    3
/    / \
4    5   6
/ \  /   / \
7   8 9  10  11
Output: 1 7 8 9 10 11 3 2 4 5 6
```

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

Approach: The idea is to use two variables i initialized to 1 and j initialized to the height of tree and run a while loop which wont break until i becomes greater than j. We will use another variable flag and initialize it to 0. Now in the while loop we will check a condition that if flag is equal to 0 we will traverse the tree from right to left and mark flag as 1 so that next time we traverse the tree from left to right and then increment the value of i so that next time we visit the level just below the current level. Also when we will traverse the level from bottom we will mark flag as 0 so that next time we traverse the tree from left to right and then decrement the value of j so that next time we visit the level just above the current level. Repeat the whole process until the binary tree is completely traversed.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Binary tree node ` `struct` `Node { ` `    ``struct` `Node* left; ` `    ``struct` `Node* right; ` `    ``int` `data; ` ` `  `    ``Node(``int` `data) ` `    ``{ ` `        ``this``->data = data; ` `        ``this``->left = NULL; ` `        ``this``->right = NULL; ` `    ``} ` `}; ` ` `  `// Recursive Function to find height ` `// of binary tree ` `int` `height(``struct` `Node* root) ` `{ ` `    ``// Base condition ` `    ``if` `(root == NULL) ` `        ``return` `0; ` ` `  `    ``// Compute the height of each subtree ` `    ``int` `lheight = height(root->left); ` `    ``int` `rheight = height(root->right); ` ` `  `    ``// Return the maximum of two ` `    ``return` `max(1 + lheight, 1 + rheight); ` `} ` ` `  `// Function to Print Nodes from left to right ` `void` `leftToRight(``struct` `Node* root, ``int` `level) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` ` `  `    ``if` `(level == 1) ` `        ``cout << root->data << ``" "``; ` ` `  `    ``else` `if` `(level > 1) { ` `        ``leftToRight(root->left, level - 1); ` `        ``leftToRight(root->right, level - 1); ` `    ``} ` `} ` ` `  `// Function to Print Nodes from right to left ` `void` `rightToLeft(``struct` `Node* root, ``int` `level) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` ` `  `    ``if` `(level == 1) ` `        ``cout << root->data << ``" "``; ` ` `  `    ``else` `if` `(level > 1) { ` `        ``rightToLeft(root->right, level - 1); ` `        ``rightToLeft(root->left, level - 1); ` `    ``} ` `} ` ` `  `// Function to print anti clockwise spiral  ` `// traversal of a binary tree ` `void` `antiClockWiseSpiral(``struct` `Node* root) ` `{ ` `    ``int` `i = 1; ` `    ``int` `j = height(root); ` ` `  `    ``// Flag to mark a change in the direction  ` `    ``// of printing nodes ` `    ``int` `flag = 0; ` `    ``while` `(i <= j) { ` ` `  `        ``// If flag is zero print nodes ` `        ``// from right to left ` `        ``if` `(flag == 0) { ` `            ``rightToLeft(root, i); ` ` `  `            ``// Set the value of flag as zero ` `            ``// so that nodes are next time  ` `            ``// printed from left to right ` `            ``flag = 1; ` ` `  `            ``// Increment i ` `            ``i++; ` `        ``} ` ` `  `        ``// If flag is one print nodes ` `        ``// from left to right ` `        ``else` `{ ` `            ``leftToRight(root, j); ` ` `  `            ``// Set the value of flag as zero ` `            ``// so that nodes are next time  ` `            ``// printed from right to left ` `            ``flag = 0; ` ` `  `            ``// Decrement j ` `            ``j--; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``struct` `Node* root = ``new` `Node(1); ` `    ``root->left = ``new` `Node(2); ` `    ``root->right = ``new` `Node(3); ` `    ``root->left->left = ``new` `Node(4); ` `    ``root->right->left = ``new` `Node(5); ` `    ``root->right->right = ``new` `Node(7); ` `    ``root->left->left->left = ``new` `Node(10); ` `    ``root->left->left->right = ``new` `Node(11); ` `    ``root->right->right->left = ``new` `Node(8); ` ` `  `    ``antiClockWiseSpiral(root); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GfG  ` `{  ` ` `  `// Binary tree node  ` `static` `class` `Node  ` `{  ` `    ``Node left;  ` `    ``Node right;  ` `    ``int` `data;  ` ` `  `    ``Node(``int` `data)  ` `    ``{  ` `        ``this``.data = data;  ` `        ``this``.left = ``null``;  ` `        ``this``.right = ``null``;  ` `    ``}  ` `} ` ` `  `// Recursive Function to find height  ` `// of binary tree  ` `static` `int` `height(Node root)  ` `{  ` `    ``// Base condition  ` `    ``if` `(root == ``null``)  ` `        ``return` `0``;  ` ` `  `    ``// Compute the height of each subtree  ` `    ``int` `lheight = height(root.left);  ` `    ``int` `rheight = height(root.right);  ` ` `  `    ``// Return the maximum of two  ` `    ``return` `Math.max(``1` `+ lheight, ``1` `+ rheight);  ` `}  ` ` `  `// Function to Print Nodes from left to right  ` `static` `void` `leftToRight(Node root, ``int` `level)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return``;  ` ` `  `    ``if` `(level == ``1``)  ` `        ``System.out.print(root.data + ``" "``);  ` ` `  `    ``else` `if` `(level > ``1``)  ` `    ``{  ` `        ``leftToRight(root.left, level - ``1``);  ` `        ``leftToRight(root.right, level - ``1``);  ` `    ``}  ` `}  ` ` `  `// Function to Print Nodes from right to left  ` `static` `void` `rightToLeft(Node root, ``int` `level)  ` `{  ` `    ``if` `(root == ``null``)  ` `        ``return``;  ` ` `  `    ``if` `(level == ``1``)  ` `        ``System.out.print(root.data + ``" "``);  ` ` `  `    ``else` `if` `(level > ``1``) ` `    ``{  ` `        ``rightToLeft(root.right, level - ``1``);  ` `        ``rightToLeft(root.left, level - ``1``);  ` `    ``}  ` `}  ` ` `  `// Function to print anti clockwise spiral  ` `// traversal of a binary tree  ` `static` `void` `antiClockWiseSpiral(Node root)  ` `{  ` `    ``int` `i = ``1``;  ` `    ``int` `j = height(root);  ` ` `  `    ``// Flag to mark a change in the direction  ` `    ``// of printing nodes  ` `    ``int` `flag = ``0``;  ` `    ``while` `(i <= j)  ` `    ``{  ` ` `  `        ``// If flag is zero print nodes  ` `        ``// from right to left  ` `        ``if` `(flag == ``0``)  ` `        ``{  ` `            ``rightToLeft(root, i);  ` ` `  `            ``// Set the value of flag as zero  ` `            ``// so that nodes are next time  ` `            ``// printed from left to right  ` `            ``flag = ``1``;  ` ` `  `            ``// Increment i  ` `            ``i++;  ` `        ``}  ` ` `  `        ``// If flag is one print nodes  ` `        ``// from left to right  ` `        ``else` `{  ` `            ``leftToRight(root, j);  ` ` `  `            ``// Set the value of flag as zero  ` `            ``// so that nodes are next time  ` `            ``// printed from right to left  ` `            ``flag = ``0``;  ` ` `  `            ``// Decrement j  ` `            ``j--;  ` `        ``}  ` `    ``}  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``Node root = ``new` `Node(``1``);  ` `    ``root.left = ``new` `Node(``2``);  ` `    ``root.right = ``new` `Node(``3``);  ` `    ``root.left.left = ``new` `Node(``4``);  ` `    ``root.right.left = ``new` `Node(``5``);  ` `    ``root.right.right = ``new` `Node(``7``);  ` `    ``root.left.left.left = ``new` `Node(``10``);  ` `    ``root.left.left.right = ``new` `Node(``11``);  ` `    ``root.right.right.left = ``new` `Node(``8``);  ` ` `  `    ``antiClockWiseSpiral(root);  ` `}  ` `}  ` ` `  `// This code is contributed by Prerna Saini. `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Binary tree node  ` `class` `newNode:  ` ` `  `    ``# Constructor to create a newNode  ` `    ``def` `__init__(``self``, data):  ` `        ``self``.data ``=` `data  ` `        ``self``.left ``=` `None` `        ``self``.right ``=` `None` `        ``self``.visited ``=` `False` `         `  `# Recursive Function to find height  ` `# of binary tree  ` `def` `height(root): ` ` `  `    ``# Base condition  ` `    ``if` `(root ``=``=` `None``):  ` `        ``return` `0` ` `  `    ``# Compute the height of each subtree  ` `    ``lheight ``=` `height(root.left)  ` `    ``rheight ``=` `height(root.right)  ` ` `  `    ``# Return the maximum of two  ` `    ``return` `max``(``1` `+` `lheight, ``1` `+` `rheight)  ` ` `  `# Function to PrNodes from left to right  ` `def` `leftToRight(root, level): ` ` `  `    ``if` `(root ``=``=` `None``):  ` `        ``return` ` `  `    ``if` `(level ``=``=` `1``): ` `        ``print``(root.data, end ``=` `" "``)  ` ` `  `    ``elif` `(level > ``1``):  ` `        ``leftToRight(root.left, level ``-` `1``)  ` `        ``leftToRight(root.right, level ``-` `1``)  ` `     `  `# Function to PrNodes from right to left  ` `def` `rightToLeft(root, level): ` ` `  `    ``if` `(root ``=``=` `None``) : ` `        ``return` ` `  `    ``if` `(level ``=``=` `1``): ` `        ``print``(root.data, end ``=` `" "``) ` ` `  `    ``elif` `(level > ``1``): ` `        ``rightToLeft(root.right, level ``-` `1``)  ` `        ``rightToLeft(root.left, level ``-` `1``)  ` `     `  `# Function to print anti clockwise spiral  ` `# traversal of a binary tree  ` `def` `antiClockWiseSpiral(root):  ` ` `  `    ``i ``=` `1` `    ``j ``=` `height(root)  ` ` `  `    ``# Flag to mark a change in the  ` `    ``# direction of printing nodes  ` `    ``flag ``=` `0` `    ``while` `(i <``=` `j): ` ` `  `        ``# If flag is zero prnodes  ` `        ``# from right to left  ` `        ``if` `(flag ``=``=` `0``):  ` `            ``rightToLeft(root, i)  ` ` `  `            ``# Set the value of flag as zero  ` `            ``# so that nodes are next time  ` `            ``# printed from left to right  ` `            ``flag ``=` `1` ` `  `            ``# Increment i  ` `            ``i ``+``=` `1` `             `  `        ``# If flag is one prnodes  ` `        ``# from left to right  ` `        ``else``: ` `            ``leftToRight(root, j)  ` ` `  `            ``# Set the value of flag as zero  ` `            ``# so that nodes are next time  ` `            ``# printed from right to left  ` `            ``flag ``=` `0` ` `  `            ``# Decrement j  ` `            ``j``-``=``1` `         `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``root ``=` `newNode(``1``)  ` `    ``root.left ``=` `newNode(``2``)  ` `    ``root.right ``=` `newNode(``3``)  ` `    ``root.left.left ``=` `newNode(``4``)  ` `    ``root.right.left ``=` `newNode(``5``)  ` `    ``root.right.right ``=` `newNode(``7``)  ` `    ``root.left.left.left ``=` `newNode(``10``)  ` `    ``root.left.left.right ``=` `newNode(``11``)  ` `    ``root.right.right.left ``=` `newNode(``8``)  ` ` `  `    ``antiClockWiseSpiral(root) ` ` `  `# This code is contributed by ` `# SHUBHAMSINGH10 `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Binary tree node ` `public` `class` `Node  ` `{ ` `    ``public` `Node left; ` `    ``public` `Node right; ` `    ``public` `int` `data; ` ` `  `    ``public` `Node(``int` `data) ` `    ``{ ` `        ``this``.data = data; ` `        ``this``.left = ``null``; ` `        ``this``.right = ``null``; ` `    ``} ` `}; ` ` `  `// Recursive Function to find height ` `// of binary tree ` `static` `int` `height( Node root) ` `{ ` `    ``// Base condition ` `    ``if` `(root == ``null``) ` `        ``return` `0; ` ` `  `    ``// Compute the height of each subtree ` `    ``int` `lheight = height(root.left); ` `    ``int` `rheight = height(root.right); ` ` `  `    ``// Return the maximum of two ` `    ``return` `Math.Max(1 + lheight, 1 + rheight); ` `} ` ` `  `// Function to Print Nodes from left to right ` `static` `void` `leftToRight( Node root, ``int` `level) ` `{ ` `    ``if` `(root == ``null``) ` `        ``return``; ` ` `  `    ``if` `(level == 1) ` `        ``Console.Write( root.data + ``" "``); ` ` `  `    ``else` `if` `(level > 1) ` `    ``{ ` `        ``leftToRight(root.left, level - 1); ` `        ``leftToRight(root.right, level - 1); ` `    ``} ` `} ` ` `  `// Function to Print Nodes from right to left ` `static` `void` `rightToLeft( Node root, ``int` `level) ` `{ ` `    ``if` `(root == ``null``) ` `        ``return``; ` ` `  `    ``if` `(level == 1) ` `        ``Console.Write( root.data + ``" "``); ` ` `  `    ``else` `if` `(level > 1)  ` `    ``{ ` `        ``rightToLeft(root.right, level - 1); ` `        ``rightToLeft(root.left, level - 1); ` `    ``} ` `} ` ` `  `// Function to print anti clockwise spiral  ` `// traversal of a binary tree ` `static` `void` `antiClockWiseSpiral( Node root) ` `{ ` `    ``int` `i = 1; ` `    ``int` `j = height(root); ` ` `  `    ``// Flag to mark a change in the direction  ` `    ``// of printing nodes ` `    ``int` `flag = 0; ` `    ``while` `(i <= j) ` `    ``{ ` ` `  `        ``// If flag is zero print nodes ` `        ``// from right to left ` `        ``if` `(flag == 0) ` `        ``{ ` `            ``rightToLeft(root, i); ` ` `  `            ``// Set the value of flag as zero ` `            ``// so that nodes are next time  ` `            ``// printed from left to right ` `            ``flag = 1; ` ` `  `            ``// Increment i ` `            ``i++; ` `        ``} ` ` `  `        ``// If flag is one print nodes ` `        ``// from left to right ` `        ``else` `        ``{ ` `            ``leftToRight(root, j); ` ` `  `            ``// Set the value of flag as zero ` `            ``// so that nodes are next time  ` `            ``// printed from right to left ` `            ``flag = 0; ` ` `  `            ``// Decrement j ` `            ``j--; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``Node root = ``new` `Node(1); ` `    ``root.left = ``new` `Node(2); ` `    ``root.right = ``new` `Node(3); ` `    ``root.left.left = ``new` `Node(4); ` `    ``root.right.left = ``new` `Node(5); ` `    ``root.right.right = ``new` `Node(7); ` `    ``root.left.left.left = ``new` `Node(10); ` `    ``root.left.left.right = ``new` `Node(11); ` `    ``root.right.right.left = ``new` `Node(8); ` ` `  `    ``antiClockWiseSpiral(root); ` ` `  `} ` `} ` ` `  `//This code is contributed by Arnab Kundu `

Output:

```1 10 11 8 3 2 4 5 7
```

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 :
Practice Tags :

1

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