Zig Zag Level order traversal of a tree using single array
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 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 nodestruct 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 traversalvoid 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 codeint 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;} |
Java
// Java program to print level order traversal// in spiral form using a single dequeueclass Solution{ static class Node{ int data; Node left, right;};// A utility function to create a new nodestatic Node newNode(int data){ Node node = new Node(); node.data = data; node.left = node.right = null; return (node);}// function to print the level order traversalstatic void levelOrder( 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 Node queue[] = new Node[2 * n]; for(int i = 0; i < 2 * n; i++) queue[i] = new Node(); int top = -1; int front = 1; queue[++top] = null; queue[++top] = root; queue[++top] = null; // Node t=root; int prevFront = 0, count = 1; while (true) { 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++) System.out.printf("%d ", queue[i].data); } else { for (int i = front - 1; i > prevFront; i--) System.out.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++) System.out.printf("%d ", queue[i].data); } else { for (int i = top - 1; i > prevFront; i--) System.out.printf("%d ", queue[i].data); }}// Driver codepublic static void main(String args[]){ 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);}}// This code is contributed by Arnab Kundu |
C#
// C# program to print level order traversal// in spiral form using a single dequeueusing System;class GFG{ public class Node{ public int data; public Node left, right;};// A utility function to create a new nodestatic Node newNode(int data){ Node node = new Node(); node.data = data; node.left = node.right = null; return (node);}// function to print the level order traversalstatic void levelOrder( 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 Node []queue = new Node[2 * n]; for(int i = 0; i < 2 * n; i++) queue[i] = new Node(); int top = -1; int front = 1; queue[++top] = null; queue[++top] = root; queue[++top] = null; // Node t=root; int prevFront = 0, count = 1; while (true) { 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++) Console.Write(" " + queue[i].data); } else { for (int i = front - 1; i > prevFront; i--) Console.Write(" " + 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++) Console.Write(" " + queue[i].data); } else { for (int i = top - 1; i > prevFront; i--) Console.Write(" " + queue[i].data); }}// Driver codepublic static void Main(String []args){ 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);}}// This code is contributed by gauravrajput1 |
Javascript
<script>// Javascript program to print level order// traversal in spiral form using a single dequeueclass Node{ constructor() { this.data = 0; this.left = null; this.right = null; }};// A utility function to create a new nodefunction newNode(data){ var node = new Node(); node.data = data; node.left = node.right = null; return (node);}// Function to print the level order traversalfunction levelOrder(root, n){ // We can just take the size as H+N which // implies the height of the tree with the // size of the tree var queue = Array(2 * n); for(var i = 0; i < 2 * n; i++) queue[i] = new Node(); var top = -1; var front = 1; queue[++top] = null; queue[++top] = root; queue[++top] = null; // Node t=root; var prevFront = 0, count = 1; while (true) { var 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(var i = prevFront + 1; i < front; i++) document.write(" " + queue[i].data); } else { for(var i = front - 1; i > prevFront; i--) document.write(" " + 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(var i = prevFront + 1; i < top; i++) document.write(" " + queue[i].data); } else { for(var i = top - 1; i > prevFront; i--) document.write(" " + queue[i].data); }}// Driver codevar 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);// This code is contributed by rutvik_56</script> |
1 2 3 4 5 6 7
Time Complexity: O(n)
Auxiliary Space : O(2*n) = O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


