@Saurabh2
the sample output is wrong…they hadn’t printed on new line each level.
For same lgoic i have taken a temp variable which will be incremented at the new level and change the order of statements.
but not working according to my thinking
Level order zigzag .wrong output case and my submission also
@piyush.bansal8158625 hey piyush this question is implemented using stacks try with stack else ping me I will help you out
whats the logic for using stack explain with a simple tree example how stack is helpful in this case
@piyush.bansal8158625 hey piyush
Assume the two stacks are current: currentlevel and nextlevel. We would also need a variable to keep track of the current level order(whether it is left to right or right to left). We pop from the currentlevel stack and print the nodes value. Whenever the current level order is from left to right, push the nodes left child, then its right child to the stack nextlevel. Since a stack is a LIFO(Last-In-First_out) structure, next time when nodes are popped off nextlevel, it will be in the reverse order. On the other hand, when the current level order is from right to left, we would push the nodes right child first, then its left child. Finally, do-not forget to swap those two stacks at the end of each level(i.e., when current level is empty)