Given a Complete Binary Tree, the task is to print the elements in the Clockwise traversal order.
Clockwise Traversal of a tree is defined as:
For the above binary tree, the Clockwise Traingular traversal will be
0, 2, 6, 14, 13, 12, 11, 10, 9, 8, 7, 3, 1, 5, 4
Input: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ /\ 8 9 10 11 Output: 1, 3, 7, 11, 10, 9, 8, 4, 2, 6, 5 Input: 1 / \ 2 3 Output: 1, 3, 2
Create a vector tree where tree[i] will store all the nodes of the tree at level i. Take an integer k which keeps track which level we are traversing other integer and cycle in which keep tracks how many cycles have been completed. Now, start printing the nodes the rightmost remaining node which has not been traversed yet & keep moving down until you reach down to the last level which has not been traversed now print this level from right to left, then move print leftmost remaining leftmost element of each level starting from last level to moving to the uppermost level whose elements has all not been traversed yet, now again do the same thing until all elements have not been traversed.
Below is the implementation of the above approach:
1 3 7 12 11 10 9 8 4 2 6 5
Time Complexity: O(n)
- Clockwise Spiral Traversal of Binary Tree
- Clockwise Spiral Traversal of Binary Tree | Set - 2
- 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
- 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
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Mix Order Traversal of a Binary Tree
- Diagonal Traversal of Binary Tree
- Density of Binary Tree in One Traversal
- Boundary Traversal of binary tree
- Zig-Zag traversal of a Binary Tree using Recursion
- Sideways traversal of a Complete Binary Tree
- Middle To Up-Down Order traversal of a Binary Tree
- Double Order Traversal of a Binary Tree
- Flatten Binary Tree in order of Zig Zag traversal
- Reverse zigzag Traversal of a Binary Tree
- Left-Right traversal of all the levels of 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.