Level order traversal in spiral form
Write a program to print Level Order Traversal in spiral form of a binary tree.
For the above tree: Basic level order traversal: 2 7 5 2 6 9 5 11 4 Level order traversal in spiral form: 2 7 5 (left to right) 9 6 2 (right to left) 5 11 4 (again left to right)
The solution will, of course, surround basic level order traversal. The spiral order means - It will go from left to right for one level, then right to left for next level and again left to right for the next one and so on.
We need to modify our basic level order traversal.
We can do the flipping of direction (left → right then right → left so on ...) by keeping a flag variable which will be updated at end of each level.
Pre-requisite: Root to tree
Example with Explanation: