Given a Binary Tree. The task is to print the circular clockwise spiral order traversal of the given binary tree.

For the above binary tree, the circular clockwise spiral order traversal will be 1, 4, 5, 6, 7, 2, 3.

Examples:
Input :
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Output : 10, 24, 23, 22, 21, 12, 13, 15, 14
Approach:
- First calculate the width of the given tree.
- Create an auxiliary 2D array of order (width*width)
- Do level order traversal of the binary tree and store levels in the newly created 2D matrix one by one in respective rows. That is, store nodes at level 0 at row indexed 0, nodes at level 1 at row indexed 1 and so on.
- Finally, traverse the 2d array in the below fashion:
- Start from the first row from left to right and print elements.
- Then traverse the last row from right to left and print elements.
- Again traverse the second row from left to right and print.
- Then second last row from right to left and so on and repeat the steps until the complete 2-D array is traversed.
Below is the implementation of the above approach:
// C++ program for Clockwise Spiral Traversal // of Binary Tree #include <bits/stdc++.h> using namespace std; // A Tree node struct Node { int key; struct Node *left, *right; }; // Utility function to create a new node Node* newNode(int key) { Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return (temp); } // Function to find the width of tree void findWidth(struct Node* node, int& maxValue, int& minValue, int hd) { if (node == NULL) return; if (hd > maxValue) { maxValue = hd; } if (hd < minValue) { minValue = hd; } findWidth(node->left, maxValue, minValue, hd - 1); findWidth(node->right, maxValue, minValue, hd + 1); } // Function to traverse the tree and // store level order traversal in a matrix void BFS(int** mtrx, struct Node* node) { // Create queue for storing // the addresses of nodes queue<struct Node*> qu; qu.push(node); int i = -1, j = -1; struct Node* poped_node = NULL; while (!qu.empty()) { i++; int qsize = qu.size(); while (qsize--) { j++; poped_node = qu.front(); // Store data of node into the matrix mtrx[i][j] = poped_node->key; qu.pop(); if (poped_node->left != NULL) { qu.push(poped_node->left); } if (poped_node->right != NULL) { qu.push(poped_node->right); } } j = -1; } } // Function for Clockwise Spiral Traversal // of Binary Tree void traverse_matrix(int** mtrx, int width) { int j = 0, k1 = 0, k2 = 0, k3 = width - 1; int k4 = width - 1; for (int round = 0; round < width / 2; round++) { for (j = k2; j < width; j++) { // only print those values which // are not MAX_INTEGER if (mtrx[k1][j] != INT_MAX) { cout << mtrx[k1][j] << ", "; } } k2 = 0; k1++; for (j = k4; j >= 0; j--) { // only print those values which are // not MAX_INTEGER if (mtrx[k3][j] != INT_MAX) { cout << mtrx[k3][j] << ", "; } } k4 = width - 1; k3--; } // condition (one row may be left traversing) // if number of rows in matrix are odd if (width % 2 != 0) { for (j = k2; j < width; j++) { // only print those values which are // not MAX_INTEGER if (mtrx[k1][j] != INT_MAX) { cout << mtrx[k1][j] << ", "; } } } } // A utility function to print clockwise // spiral traversal of tree void printPattern(struct Node* node) { // max, min has taken for // calculating width of tree int max_value = INT_MIN; int min_value = INT_MAX; int hd = 0; // calculate the width of a tree findWidth(node, max_value, min_value, hd); int width = max_value + abs(min_value); // use double pointer to create 2D array int** mtrx = new int*[width]; // initialize width for each row of matrix for (int i = 0; i < width; i++) { mtrx[i] = new int[width]; } // initialize complete matrix with // MAX INTEGER(purpose garbage) for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) { mtrx[i][j] = INT_MAX; } } // Store the BFS traversal of the tree // into the 2-D matrix BFS(mtrx, node); // Print the circular clockwise spiral // traversal of the tree traverse_matrix(mtrx, width); // release extra memory // allocated for matrix free(mtrx); } // Driver Code int main() { /* 10 / \ 12 13 / \ 14 15 / \ / \ 21 22 23 24 Let us create Binary Tree as shown in above example */ Node* root = newNode(10); root->left = newNode(12); root->right = newNode(13); root->right->left = newNode(14); root->right->right = newNode(15); root->right->left->left = newNode(21); root->right->left->right = newNode(22); root->right->right->left = newNode(23); root->right->right->right = newNode(24); cout << "Circular Clockwise Spiral Traversal : \n"; printPattern(root); return 0; } |
chevron_right
filter_none
Output:
Circular Clockwise Spiral Traversal : 10, 24, 23, 22, 21, 12, 13, 15, 14,
Recommended Posts:
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Maximum spiral sum in Binary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- Density of Binary Tree in One Traversal
- Boundary Traversal of binary tree
- Diagonal Traversal of Binary Tree
- Iterative diagonal traversal of binary tree
- If you are given two traversal sequences, can you construct the binary tree?
- Modify a binary tree to get preorder traversal using right pointers only
- Reverse Morris traversal using Threaded Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Find n-th node in Postorder traversal of a Binary Tree
- Construct Special Binary Tree from given Inorder traversal
- Postorder traversal of Binary Tree without recursion and without 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 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.



