Level order traversal in spiral form | Using Deque
Last Updated : 12 Jul, 2025
Given a binary tree and the task is to find the spiral order traversal of the tree and return the list containing the elements. Spiral order Traversal mean: Starting from level 0 for root node, for all the even levels we print the node's value from right to left and for all the odd levels we print the node's value from left to right.
Examples:
Input: root = [1, 3, 2]
Output : 1 3 2 Explanation: Start with root (1), print level 0 (right to left), then level 1 (left to right).
Input : root = [10, 20, 30, 40, 60]
Output : 10 20 30 60 40 Explanation: Start with root (10), print level 0 (right to left), level 1 (left to right), and continue alternating.
// C++ program to print level order traversal// in spiral form using one deque.#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intval){data=val;left=NULL;right=NULL;}};voidspiralOrder(Node*root){deque<Node*>d;// Push rootd.push_back(root);// Direction 0 shows print right to left// and for Direction 1 left to rightintdir=0;while(!d.empty()){intsize=d.size();while(size--){// One whole level// will be print in this loopif(dir==0){Node*temp=d.back();d.pop_back();if(temp->right)d.push_front(temp->right);if(temp->left)d.push_front(temp->left);cout<<temp->data<<" ";}else{Node*temp=d.front();d.pop_front();if(temp->left)d.push_back(temp->left);if(temp->right)d.push_back(temp->right);cout<<temp->data<<" ";}}cout<<endl;// Direction changedir=1-dir;}}intmain(){// Build the TreeNode*root=newNode(10);root->left=newNode(20);root->right=newNode(30);root->left->left=newNode(40);root->left->right=newNode(60);// Call the FunctionspiralOrder(root);return0;}
Java
// Java program to print level order traversal // in spiral form using one deque. importjava.util.*;classGFG{staticclassNode{intdata;Nodeleft,right;Node(intval){data=val;left=null;right=null;}};staticvoidspiralOrder(Noderoot){Deque<Node>d=newLinkedList<Node>();// Push root d.addLast(root);// Direction 0 shows print right to left // and for Direction 1 left to right intdir=0;while(d.size()>0){intsize=d.size();while(size-->0){// One whole level // will be print in this loop if(dir==0){Nodetemp=d.peekLast();d.pollLast();if(temp.right!=null)d.addFirst(temp.right);if(temp.left!=null)d.addFirst(temp.left);System.out.print(temp.data+" ");}else{Nodetemp=d.peekFirst();d.pollFirst();if(temp.left!=null)d.addLast(temp.left);if(temp.right!=null)d.addLast(temp.right);System.out.print(temp.data+" ");}}System.out.println();// Direction change dir=1-dir;}}// Driver codepublicstaticvoidmain(Stringargs[]){// Build the Tree Noderoot=newNode(10);root.left=newNode(20);root.right=newNode(30);root.left.left=newNode(40);root.left.right=newNode(60);// Call the Function spiralOrder(root);}}// This code is contributed by Arnab Kundu
Python3
# Python program to print level order traversal # in spiral form using one deque. classNode:def__init__(self,val):self.data=val;self.left=None;self.right=None;defspiralOrder(root):d=[];# Push root d.append(root);# Direction 0 shows print right to left # and for Direction 1 left to rightdirect=0;while(len(d)!=0):size=len(d);while(size):size-=1;# One whole level# will be print in this loopif(direct==0):temp=d.pop();if(temp.right):d.insert(0,temp.right);if(temp.left):d.insert(0,temp.left);print(temp.data,end=" ");else:temp=d[0];d.pop(0);if(temp.left):d.append(temp.left);if(temp.right):d.append(temp.right);print(temp.data,end=" ");print()# Direction change direct=1-direct;if__name__=="__main__":# Build the Tree root=Node(10);root.left=Node(20);root.right=Node(30);root.left.left=Node(40);root.left.right=Node(60);# Call the Function spiralOrder(root);# This code is contributed by AnkitRai01
C#
//C# code for the above approachusingSystem;usingSystem.Collections.Generic;classGFG{classNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=null;right=null;}}staticvoidSpiralOrder(Noderoot){Queue<Node>d=newQueue<Node>();// Push rootd.Enqueue(root);// Direction 0 shows print right to left// and for Direction 1 left to rightintdir=0;while(d.Count>0){intsize=d.Count;while(size-->0){// One whole level// will be print in this loopif(dir==1){Nodetemp=d.Peek();d.Dequeue();if(temp.right!=null)d.Enqueue(temp.right);if(temp.left!=null)d.Enqueue(temp.left);Console.Write(temp.data+" ");}else{Nodetemp=d.Peek();d.Dequeue();if(temp.left!=null)d.Enqueue(temp.left);if(temp.right!=null)d.Enqueue(temp.right);Console.Write(temp.data+" ");}}Console.WriteLine();// Direction changedir=1-dir;}}// Driver code// Driver codestaticvoidMain(string[]args){// Build the TreeNoderoot=newNode(10);root.left=newNode(20);root.right=newNode(30);root.left.left=newNode(40);root.left.right=newNode(60);// Call the FunctionSpiralOrder(root);}}//This code is contributed by Potta Lokesh
JavaScript
<script>// JavaScript program to print level order traversal// in spiral form using one deque.classNode{constructor(val){this.data=val;this.left=null;this.right=null;}};functionspiralOrder(root){vard=[];// Push rootd.push(root);// Direction 0 shows print right to left// and for Direction 1 left to rightvardir=0;while(d.length!=0){varsize=d.length;while(size-->0){// One whole level// will be print in this loopif(dir==0){vartemp=d[d.length-1];d.pop();if(temp.right!=null)d.unshift(temp.right);if(temp.left!=null)d.unshift(temp.left);document.write(temp.data+" ");}else{vartemp=d[0];d.shift();if(temp.left!=null)d.push(temp.left);if(temp.right!=null)d.push(temp.right);document.write(temp.data+" ");}}document.write("<br>");// Direction changedir=1-dir;}}// Build the Treevarroot=newNode(10);root.left=newNode(20);root.right=newNode(30);root.left.left=newNode(40);root.left.right=newNode(60);// Call the FunctionspiralOrder(root);</script>
Output
10
20 30
60 40
Time Complexity: O(N), where N is the number of Nodes Space Complexity: O(N), where N is the number of Nodes