Given a binary tree of N nodes, the task is to print level order traversal in a spiral form. In spiral form, nodes at the first and second level of tree are printed normally (left to right), after which nodes at alternate levels are printed in reverse order.
Input: N = 31 / \ 3 2
Output: 1 3 2
Nodes at level 0 printed in normal order (1)
Nodes at level 1 printed in normal order (3, 2)
Hence, spiral order is (1, 3, 2)
Input: N = 5
10 / \ 20 30 / \ 40 60
Output: 10 20 30 60 40
Nodes at level 0 printed in normal order (10)
Nodes at level 1 printed in normal order (20, 30)
Nodes at level 2 printed in reverse order (60, 40)
Hence, spiral order is (10, 20, 30, 60, 40)
A naive approach for this problem has already been discussed in this article. The basic idea is to use recursion and a flag variable, using which nodes of alternate levels are printed in reverse order and finally spiral form is obtained.
Time complexity: O(N2)
Auxiliary Space: O(1)
In this approach, stack and multimap are used. A multimap container in C++ stores the (key, value) pairs in ascending order, sorted according to the key. For every node of a given tree, if we put (level, node) in a multimap, then it will store these nodes sorted according to their level.
For example, a given tree is:
For this tree, the multimap would look like:
Key(Level) Value(Element) 0 1 1 2 1 3 2 7 2 6 2 5 2 4
Detailed steps of this approach are as follows:
- Traverse the given tree and insert all (level, node) pairs in a multimap and then traverse this multimap.
- If the level is odd, print the nodes in order in which they are present in the multimap.
- If the level is even, push all elements of the current level to a stack then pop all elements from the stack and print them. It gives the reverse order.
Finally, this level order traversal will result in the required spiral form.
Below is the implementation of the above approach:
1 2 3 4 5 6 7
Time complexity: O(NlogN)
Auxiliary Space: O(N)
- Level order traversal in spiral form | Using one stack and one queue
- Level order traversal in spiral form | Using Deque
- Level order traversal in spiral form
- Reverse Level Order traversal in spiral form
- multimap::crbegin() and multimap::crend() in C++ STL
- multimap::cbegin() and multimap::cend() in C++ STL
- multimap::begin() and multimap::end() in C++ STL
- Level order traversal of Binary Tree using Morris Traversal
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Connect Nodes at same Level (Level Order Traversal)
- Insertion in n-ary tree in given order and Level order traversal
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Flatten Binary Tree in order of Level Order Traversal
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Level order traversal line by line | Set 2 (Using Two Queues)
- Level order traversal line by line | Set 3 (Using One Queue)
- Check if two trees are mirror of each other using level order traversal
- Zig Zag Level order traversal of a tree using single array
- Density of Binary Tree using Level Order Traversal
- Deletion of a given node K in a Binary Tree using Level Order Traversal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.