Zig Zag Level order traversal of a tree using single queue
Write a function to print spiral order traversal of a tree. For below tree, function should print 1, 2, 3, 4, 5, 6, 7.

We have discussed naive approach and two stack based approach in Level Order with recursion and multiple stacks
The idea behind this approach is first we have to take a queue, a direction flag and a separation flag which is NULL
- Insert the root element into the queue and again insert NULL into the queue.
- For every element in the queue insert its child nodes.
- If a NULL is encountered then check the direction to traverse the particular level is left to right or right to left. If it’s an even level then traverse from left to right otherwise traverse the tree in right to level order i.e., from the front to the previous front i.e., from the current NULL to to the last NULL that has been visited. This continues till the last level then there the loop breaks and we print what is left (that has not printed) by checking the direction to print.
Following is the C++ implementation of the explanation
C++
// C++ program to print level order traversal // in spiral form using a single dequeue #include <bits/stdc++.h> struct Node { int data; struct Node *left, *right; }; // A utility function to create a new node struct Node* newNode(int data) { struct Node* node = new struct Node; node->data = data; node->left = node->right = NULL; return (node); } // function to print the level order traversal void levelOrder(struct Node* root, int n) { // We can just take the size as H+N which // implies the height of the tree with the // size of the tree struct Node* queue[2 * n]; int top = -1; int front = 1; queue[++top] = NULL; queue[++top] = root; queue[++top] = NULL; // struct Node* t=root; int prevFront = 0, count = 1; while (1) { struct Node* curr = queue[front]; // A level separator found if (curr == NULL) { // If this is the only item in dequeue if (front == top) break; // Else print contents of previous level // according to count else { if (count % 2 == 0) { for (int i = prevFront + 1; i < front; i++) printf("%d ", queue[i]->data); } else { for (int i = front - 1; i > prevFront; i--) printf("%d ", queue[i]->data); } prevFront = front; count++; front++; // Insert a new level separator queue[++top] = NULL; continue; } } if (curr->left != NULL) queue[++top] = curr->left; if (curr->right != NULL) queue[++top] = curr->right; front++; } if (count % 2 == 0) { for (int i = prevFront + 1; i < top; i++) printf("%d ", queue[i]->data); } else { for (int i = top - 1; i > prevFront; i--) printf("%d ", queue[i]->data); } } // Driver code int main() { struct Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(7); root->left->right = newNode(6); root->right->left = newNode(5); root->right->right = newNode(4); levelOrder(root, 7); return 0; } |
1 2 3 4 5 6 7
Time Complexity: O(n)
Auxiliary Space : O(2*n) = O(n)
Recommended Posts:
- Level order traversal in spiral form | Using one stack and one queue
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Level Order Tree Traversal
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Level order traversal line by line | Set 3 (Using One Queue)
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Connect Nodes at same Level (Level Order Traversal)
- Reverse Level Order Traversal
- Construct BST from its given level order traversal
- Level order traversal in spiral form
- Level order traversal with direction change after every two levels
- Check if two trees are mirror of each other 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 contribute@geeksforgeeks.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.



