Given a Binary Tree. The task is to print the circular reverse clockwise spiral order traversal of the given binary tree.
Reverse Clockwise Traversal means to traverse the tree in clockwise direction spirally starting from the bottom instead of top root node.
Input : 1 / \ 2 3 / \ \ 4 5 6 / / \ 7 8 9 Output : 9 8 7 1 6 5 4 2 3 Input : 20 / \ 8 22 / \ / \ 5 3 4 25 / \ 10 14 Output : 14 10 20 25 4 3 5 8 22
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.
- Use another variable flag and initialize it to 0. Now in the while loop, check a condition that if flag is equal to 0 then traverse the tree from right to left and mark flag as 1 so that next time we traverse the tree from left to right, this is done to maintain the spiral clockwise order of traversal.
- Also when we will traverse the level from top we will mark flag as 0 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.
- Repeat the whole process until the binary tree is completely traversed.
<liThen decrement the value of j so that next time the level just above the current level is visited.
Below is the implementation of the above approach:
7 6 5 4 1 3 2
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Reverse Anti Clockwise Spiral Traversal of a Binary Tree
- Clockwise Spiral Traversal of Binary Tree | Set - 2
- Clockwise Spiral Traversal of Binary Tree
- Anti Clockwise spiral traversal of a binary tree
- Clockwise Triangular traversal of a Binary Tree
- Reverse zigzag Traversal of a Binary Tree
- Reverse Morris traversal using Threaded Binary Tree
- Reverse Level Order traversal in spiral form
- 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
- Zig-Zag traversal of a Binary Tree using Recursion
- Diagonal Traversal of Binary Tree
- Boundary Traversal of binary tree
- Density of Binary Tree in One Traversal
- Sideways traversal of a Complete Binary Tree
- Flatten Binary Tree in order of Zig Zag traversal