Given a Binary Tree. The task is to print the circular clockwise spiral order traversal of the given binary tree.
Input : 1 / \ 2 3 / \ \ 4 5 6 / / \ 7 8 9 Output :1 9 8 7 2 3 6 5 4 Input : 20 / \ 8 22 / \ / \ 5 3 4 25 / \ 10 14 Output :20 14 10 8 22 25 4 3 5
We have already discussed Clockwise spiral traversal of binary tree using a 2D array.Here we will discuss another approach which will not use 2D array.
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 left to right and mark flag as 1 so that next time we traverse the tree from right to left 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 right to left 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:
10 24 23 22 21 12 13 15 14
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.
- Clockwise Spiral Traversal of Binary Tree
- Reverse Clockwise spiral traversal of a binary tree
- Anti Clockwise spiral traversal of a binary tree
- Reverse Anti Clockwise Spiral Traversal of a Binary Tree
- Clockwise Triangular traversal of a Binary Tree
- 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
- 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
- Mix Order Traversal of a Binary Tree
- Zig-Zag traversal of a Binary Tree using Recursion
- Density of Binary Tree in One Traversal
- Diagonal Traversal of Binary Tree
- Boundary Traversal of binary tree
- Iterative diagonal traversal of binary tree
- If you are given two traversal sequences, can you construct the binary tree?
- Double Order Traversal of a Binary Tree
- Triple Order Traversal of a Binary Tree
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.