• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
July 15, 2024 |10.5K Views

Level order traversal in spiral form

  Share  1 Like
Description
Discussion

This problem can be seen as an extension of the level order traversal post. 
To print the nodes in spiral order, nodes at different levels should be printed in alternating order. An additional Boolean variable ltr is used to change printing order of levels. If ltr is 1 then printGivenLevel() prints nodes from left to right else from right to left. Value of ltr is flipped in each iteration to change the order.
Function to print level order traversal of tree

Level order traversal in spiral form: https://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/