Given the root of a binary tree, perform a zigzag (spiral) level order traversal. For odd-numbered levels, traverse the nodes from left to right and for even-numbered levels, traverse the nodes from right to left.
The idea is to first calculate the height of the tree, and then recursively traverse each level to perform a level order traversal according to the current level’s direction.
The traversal alternates directions at each level to achieve a zigzag pattern:
At the first level, nodes are visited from left to right.
At the next level, nodes are visited from right to left.
This pattern continues for all levels by flipping the direction after each level.
C++
#include<vector>#include<iostream>usingnamespacestd;// Node StructureclassNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Finding the Tree HeightinttreeHeight(Node*root){if(!root)return0;intlHeight=treeHeight(root->left);intrHeight=treeHeight(root->right);returnmax(lHeight,rHeight)+1;}// Function which prints from left to // right traversal at a certain levelvoidleftToRightTrav(Node*root,intlevel,vector<int>&res){if(root==nullptr)return;if(level==1){res.push_back(root->data);}else{leftToRightTrav(root->left,level-1,res);leftToRightTrav(root->right,level-1,res);}}// Function which prints from right to // left traversal at a certain levelvoidrightToLeftTrav(Node*root,intlevel,vector<int>&res){if(root==nullptr)return;if(level==1){res.push_back(root->data);}else{rightToLeftTrav(root->right,level-1,res);rightToLeftTrav(root->left,level-1,res);}}vector<int>zigZagTraversal(Node*root){vector<int>res;boolleftToRight=true;intheight=treeHeight(root);for(inti=1;i<=height;i++){if(leftToRight)leftToRightTrav(root,i,res);elserightToLeftTrav(root,i,res);// Flip the value of leftToRightleftToRight=!leftToRight;}returnres;}intmain(){// Create a input binary tree// 20// / \ // 8 22// / \ \ // 4 12 11// / \ // 10 14Node*root=newNode(20);root->left=newNode(8);root->right=newNode(22);root->right->right=newNode(11);root->left->left=newNode(4);root->left->right=newNode(12);root->left->right->left=newNode(10);root->left->right->right=newNode(14);vector<int>res=zigZagTraversal(root);for(inti=0;i<res.size();i++){cout<<res[i]<<" ";}return0;}
C
#include<stdio.h>#include<stdlib.h>#include<stdbool.h>// Node StructurestructNode{intdata;structNode*left;structNode*right;};// Finding the Tree HeightinttreeHeight(structNode*root){if(root==NULL)return0;intlHeight=treeHeight(root->left);intrHeight=treeHeight(root->right);return(lHeight>rHeight?lHeight:rHeight)+1;}// Function to traverse from left to right traversal at a certain levelvoidleftToRightTrav(structNode*root,intlevel,int*res,int*index){if(root==NULL)return;if(level==1){res[(*index)++]=root->data;}else{leftToRightTrav(root->left,level-1,res,index);leftToRightTrav(root->right,level-1,res,index);}}// Function to traverse from right to left traversal at a certain levelvoidrightToLeftTrav(structNode*root,intlevel,int*res,int*index){if(root==NULL)return;if(level==1){res[(*index)++]=root->data;}else{rightToLeftTrav(root->right,level-1,res,index);rightToLeftTrav(root->left,level-1,res,index);}}// Function to traverse the tree in zigzag ordervoidzigZagTraversal(structNode*root,int*res,int*size){boolleftToRight=true;intheight=treeHeight(root);intindex=0;for(inti=1;i<=height;i++){if(leftToRight)leftToRightTrav(root,i,res,&index);elserightToLeftTrav(root,i,res,&index);leftToRight=!leftToRight;}// Set the size of the result*size=index;}structNode*createNode(intval){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=val;node->left=node->right=NULL;returnnode;}intmain(){// Create a input binary tree// 20// / \ // 8 22// / \ \ // 4 12 11// / \ // 10 14structNode*root=createNode(20);root->left=createNode(8);root->right=createNode(22);root->right->right=createNode(11);root->left->left=createNode(4);root->left->right=createNode(12);root->left->right->left=createNode(10);root->left->right->right=createNode(14);// Array to hold zigzag traversal resultsintres[200];intsize=0;zigZagTraversal(root,res,&size);for(inti=0;i<size;i++){printf("%d ",res[i]);}printf("\n");return0;}
Java
importjava.util.ArrayList;importjava.util.List;// Node StructureclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGFG{// Finding the Tree HeightstaticinttreeHeight(Noderoot){if(root==null)return0;intlHeight=treeHeight(root.left);intrHeight=treeHeight(root.right);returnMath.max(lHeight,rHeight)+1;}// Function which prints from left to // right traversal at a certain levelstaticvoidleftToRightTrav(Noderoot,intlevel,ArrayList<Integer>res){if(root==null)return;if(level==1){res.add(root.data);}else{leftToRightTrav(root.left,level-1,res);leftToRightTrav(root.right,level-1,res);}}// Function which prints from right to // left traversal at a certain levelstaticvoidrightToLeftTrav(Noderoot,intlevel,ArrayList<Integer>res){if(root==null)return;if(level==1){res.add(root.data);}else{rightToLeftTrav(root.right,level-1,res);rightToLeftTrav(root.left,level-1,res);}}// Finding the zigZagTraversalstaticArrayList<Integer>zigZagTraversal(Noderoot){ArrayList<Integer>res=newArrayList<>();booleanleftToRight=true;intheight=treeHeight(root);for(inti=1;i<=height;i++){if(leftToRight)leftToRightTrav(root,i,res);elserightToLeftTrav(root,i,res);// Flip the value of leftToRightleftToRight=!leftToRight;}returnres;}publicstaticvoidmain(String[]args){// Create a input binary tree// 20// / \// 8 22// / \ \// 4 12 11// / \// 10 14Noderoot=newNode(20);root.left=newNode(8);root.right=newNode(22);root.right.right=newNode(11);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);ArrayList<Integer>res=zigZagTraversal(root);for(inti:res){System.out.print(i+" ");}System.out.println();}}
Python
# Node StructureclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Finding the Tree HeightdeftreeHeight(root):ifrootisNone:return0lHeight=treeHeight(root.left)rHeight=treeHeight(root.right)returnmax(lHeight,rHeight)+1# Function which prints from left to # right traversal at a certain leveldefleftToRightTrav(root,level,res):ifrootisNone:returniflevel==1:res.append(root.data)else:leftToRightTrav(root.left,level-1,res)leftToRightTrav(root.right,level-1,res)# Function which prints from right to # left traversal at a certain leveldefrightToLeftTrav(root,level,res):ifrootisNone:returniflevel==1:res.append(root.data)else:rightToLeftTrav(root.right,level-1,res)rightToLeftTrav(root.left,level-1,res)# Finding the zigZagTraversaldefzigZagTraversal(root):res=[]leftToRight=Trueheight=treeHeight(root)foriinrange(1,height+1):ifleftToRight:leftToRightTrav(root,i,res)else:rightToLeftTrav(root,i,res)# Flip the value of leftToRightleftToRight=notleftToRightreturnresif__name__=="__main__":# Create a input binary tree# 20# / \# 8 22# / \ \# 4 12 11# / \# 10 14root=Node(20)root.left=Node(8)root.right=Node(22)root.right.right=Node(11)root.left.left=Node(4)root.left.right=Node(12)root.left.right.left=Node(10)root.left.right.right=Node(14)res=zigZagTraversal(root)foriinres:print(i,end=" ")print()
C#
usingSystem;usingSystem.Collections.Generic;// Node StructureclassNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGFG{// Finding the Tree HeightstaticinttreeHeight(Noderoot){if(root==null)return0;intlHeight=treeHeight(root.left);intrHeight=treeHeight(root.right);returnMath.Max(lHeight,rHeight)+1;}// Function which prints from left to // right traversal at a certain levelstaticvoidleftToRightTrav(Noderoot,intlevel,List<int>res){if(root==null)return;if(level==1){res.Add(root.data);}else{leftToRightTrav(root.left,level-1,res);leftToRightTrav(root.right,level-1,res);}}// Function which prints from right to // left traversal at a certain levelstaticvoidrightToLeftTrav(Noderoot,intlevel,List<int>res){if(root==null)return;if(level==1){res.Add(root.data);}else{rightToLeftTrav(root.right,level-1,res);rightToLeftTrav(root.left,level-1,res);}}// Finding the zigZagTraversalstaticList<int>zigZagTraversal(Noderoot){List<int>res=newList<int>();boolleftToRight=true;intheight=treeHeight(root);for(inti=1;i<=height;i++){if(leftToRight)leftToRightTrav(root,i,res);elserightToLeftTrav(root,i,res);// Flip the value of leftToRightleftToRight=!leftToRight;}returnres;}staticvoidMain(){// Create a input binary tree// 20// / \// 8 22// / \ \// 4 12 11// / \// 10 14Noderoot=newNode(20);root.left=newNode(8);root.right=newNode(22);root.right.right=newNode(11);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);List<int>res=zigZagTraversal(root);foreach(intiinres){Console.Write(i+" ");}Console.WriteLine();}}
JavaScript
// Node StructureclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Finding the Tree HeightfunctiontreeHeight(root){if(root===null)return0;letlHeight=treeHeight(root.left);letrHeight=treeHeight(root.right);returnMath.max(lHeight,rHeight)+1;}// Function which prints from left to // right traversal at a certain levelfunctionleftToRightTrav(root,level,res){if(root===null)return;if(level===1){res.push(root.data);}else{leftToRightTrav(root.left,level-1,res);leftToRightTrav(root.right,level-1,res);}}// Function which prints from right to // left traversal at a certain levelfunctionrightToLeftTrav(root,level,res){if(root===null)return;if(level===1){res.push(root.data);}else{rightToLeftTrav(root.right,level-1,res);rightToLeftTrav(root.left,level-1,res);}}functionzigZagTraversal(root){letres=[];letleftToRight=true;letheight=treeHeight(root);for(leti=1;i<=height;i++){if(leftToRight){leftToRightTrav(root,i,res);}else{rightToLeftTrav(root,i,res);}// Flip the value of leftToRightleftToRight=!leftToRight;}returnres;}// Driver code// Create a input binary tree// 20// / \// 8 22// / \ \// 4 12 11// / \// 10 14letroot=newNode(20);root.left=newNode(8);root.right=newNode(22);root.right.right=newNode(11);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);letres=zigZagTraversal(root);res.forEach(i=>console.log(i));
Output
20 22 8 4 12 11 14 10
Time Complexity: O(n*h), where n is the number of nodes and h is the height of the tree. For each height from 1 to n, we are recursively traversing the tree from root in order to get nodes at a certain height. Auxiliary Space: O(h)
[Expected Approach - 1] - Using Two Stacks - O(n) Time and O(n) Space
The idea is to perform a zigzag (spiral) level order traversal of a binary tree using two stacks instead of recursion.
s1 stores nodes of the current level, and s2 stores nodes of the next level.
Nodes in s1 are processed from top to bottom, and their children are pushed onto s2 in left → right order.
Nodes in s2 are then processed from top to bottom, and their children are pushed onto s1 in right → left order.
By alternating the order of pushing children between the two stacks at each level, the traversal naturally alternates direction, achieving the zigzag pattern.
C++
#include<iostream>#include<vector>usingnamespacestd;// Node StructureclassNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};vector<int>zigZagTraversal(Node*root){vector<int>res;if(root==nullptr)returnres;// Current levelstack<Node*>s1;// Next levelstack<Node*>s2;s1.push(root);while(!s1.empty()||!s2.empty()){// Print nodes of current level from s1// and push nodes of next level to s2while(!s1.empty()){Node*curr=s1.top();s1.pop();res.push_back(curr->data);if(curr->left)s2.push(curr->left);if(curr->right)s2.push(curr->right);}// Print nodes of current level from s2// and push nodes of next level to s1while(!s2.empty()){Node*curr=s2.top();s2.pop();res.push_back(curr->data);if(curr->right)s1.push(curr->right);if(curr->left)s1.push(curr->left);}}returnres;}intmain(){// Create a input binary tree// 20// / \ // 8 22// / \ \ // 4 12 11// / \ // 10 14Node*root=newNode(20);root->left=newNode(8);root->right=newNode(22);root->right->right=newNode(11);root->left->left=newNode(4);root->left->right=newNode(12);root->left->right->left=newNode(10);root->left->right->right=newNode(14);vector<int>res=zigZagTraversal(root);for(autoval:res)cout<<val<<" ";cout<<endl;return0;}
Java
importjava.util.ArrayList;importjava.util.List;// Node StructureclassNode{intdata;Nodeleft;Noderight;Node(intx){data=x;left=null;right=null;}}classGFG{staticArrayList<Integer>zigZagTraversal(Noderoot){ArrayList<Integer>res=newArrayList<>();if(root==null)returnres;// Current levelStack<Node>s1=newStack<>();// Next levelStack<Node>s2=newStack<>();s1.push(root);while(!s1.empty()||!s2.empty()){// Print nodes of current level from s1// and push nodes of next level to s2while(!s1.empty()){Nodecurr=s1.pop();res.add(curr.data);if(curr.left!=null)s2.push(curr.left);if(curr.right!=null)s2.push(curr.right);}// Print nodes of current level from s2// and push nodes of next level to s1while(!s2.empty()){Nodecurr=s2.pop();res.add(curr.data);if(curr.right!=null)s1.push(curr.right);if(curr.left!=null)s1.push(curr.left);}}returnres;}publicstaticvoidmain(String[]args){// Create a input binary tree// 20// / \// 8 22// / \ \// 4 12 11// / \// 10 14Noderoot=newNode(20);root.left=newNode(8);root.right=newNode(22);root.right.right=newNode(11);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);ArrayList<Integer>res=zigZagTraversal(root);for(intval:res)System.out.print(val+" ");System.out.println();}}
Python
# Node StructureclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=NonedefzigZagTraversal(root):res=[]ifrootisNone:returnres# Current levels1=[]# Next levels2=[]s1.append(root)whiles1ors2:# Print nodes of current level from s1# and push nodes of next level to s2whiles1:curr=s1.pop()res.append(curr.data)ifcurr.left:s2.append(curr.left)ifcurr.right:s2.append(curr.right)# Print nodes of current level from s2# and push nodes of next level to s1whiles2:curr=s2.pop()res.append(curr.data)ifcurr.right:s1.append(curr.right)ifcurr.left:s1.append(curr.left)returnresif__name__=="__main__":# Create a input binary tree# 20# / \# 8 22# / \ \# 4 12 11# / \# 10 14root=Node(20)root.left=Node(8)root.right=Node(22)root.right.right=Node(11)root.left.left=Node(4)root.left.right=Node(12)root.left.right.left=Node(10)root.left.right.right=Node(14)res=zigZagTraversal(root)forvalinres:print(val,end=" ")print()
C#
usingSystem;usingSystem.Collections.Generic;// Node StructureclassNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intx){data=x;left=null;right=null;}}classGFG{staticList<int>zigZagTraversal(Noderoot){List<int>res=newList<int>();if(root==null)returnres;// Current levelStack<Node>s1=newStack<Node>();// Next levelStack<Node>s2=newStack<Node>();s1.Push(root);while(s1.Count>0||s2.Count>0){// Print nodes of current level from s1// and push nodes of next level to s2while(s1.Count>0){Nodecurr=s1.Pop();res.Add(curr.data);if(curr.left!=null)s2.Push(curr.left);if(curr.right!=null)s2.Push(curr.right);}// Print nodes of current level from s2// and push nodes of next level to s1while(s2.Count>0){Nodecurr=s2.Pop();res.Add(curr.data);if(curr.right!=null)s1.Push(curr.right);if(curr.left!=null)s1.Push(curr.left);}}returnres;}staticvoidMain(){// Create a input binary tree// 20// / \// 8 22// / \ \// 4 12 11// / \// 10 14Noderoot=newNode(20);root.left=newNode(8);root.right=newNode(22);root.right.right=newNode(11);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);List<int>res=zigZagTraversal(root);foreach(intvalinres)Console.Write(val+" ");Console.WriteLine();}}
JavaScript
// Node StructureclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}functionzigZagTraversal(root){letres=[];if(root==null)returnres;// Current levellets1=[];// Next levellets2=[];s1.push(root);while(s1.length>0||s2.length>0){// Print nodes of current level from s1// and push nodes of next level to s2while(s1.length>0){letcurr=s1.pop();res.push(curr.data);if(curr.left)s2.push(curr.left);if(curr.right)s2.push(curr.right);}// Print nodes of current level from s2// and push nodes of next level to s1while(s2.length>0){letcurr=s2.pop();res.push(curr.data);if(curr.right)s1.push(curr.right);if(curr.left)s1.push(curr.left);}}returnres;}// Driver code// Create a input binary tree// 20// / \// 8 22// / \ \// 4 12 11// / \// 10 14letroot=newNode(20);root.left=newNode(8);root.right=newNode(22);root.right.right=newNode(11);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);letres=zigZagTraversal(root);console.log(res.join(" "));
Output
20 22 8 4 12 11 14 10
[Expected Approach - 2] - Using Deque - O(n) Time and O(n) Space
The idea is to use a deque to store nodes level by level and alternate the direction of traversal at each level.
For a level traversed left to right, nodes are popped from the front, and their children are added to the back in left → right order.
For a level traversed right to left, nodes are popped from the back, and their children are added to the front in right → left order.
C++
#include<iostream>#include<vector>// Node StructureclassNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};vector<int>zigZagTraversal(Node*root){vector<int>res;if(!root)returnres;deque<Node*>dq;dq.push_back(root);boolreverse=false;while(!dq.empty()){intn=dq.size();while(n--){// Push right to the front // first if reverse is trueif(reverse){Node*curr=dq.back();dq.pop_back();res.push_back(curr->data);if(curr->right)dq.push_front(curr->right);if(curr->left)dq.push_front(curr->left);}// Else push left to the back firstelse{Node*curr=dq.front();dq.pop_front();res.push_back(curr->data);if(curr->left)dq.push_back(curr->left);if(curr->right)dq.push_back(curr->right);}}reverse=!reverse;}returnres;}intmain(){// Create a input binary tree// 20// / \ // 8 22// / \ \ // 4 12 11// / \ // 10 14Node*root=newNode(20);root->left=newNode(8);root->right=newNode(22);root->right->right=newNode(11);root->left->left=newNode(4);root->left->right=newNode(12);root->left->right->left=newNode(10);root->left->right->right=newNode(14);vector<int>res=zigZagTraversal(root);for(autoval:res)cout<<val<<" ";cout<<endl;return0;}
Java
importjava.util.ArrayList;importjava.util.List;// Node StructureclassNode{intdata;Nodeleft;Noderight;Node(intx){data=x;left=null;right=null;}}classGFG{staticArrayList<Integer>zigZagTraversal(Noderoot){ArrayList<Integer>res=newArrayList<>();if(root==null)returnres;Deque<Node>dq=newLinkedList<>();dq.addLast(root);booleanreverse=false;while(!dq.isEmpty()){intn=dq.size();while(n-->0){// Push right to the front // first if reverse is trueif(reverse){Nodecurr=dq.removeLast();res.add(curr.data);if(curr.right!=null)dq.addFirst(curr.right);if(curr.left!=null)dq.addFirst(curr.left);}// Else push left to the back firstelse{Nodecurr=dq.removeFirst();res.add(curr.data);if(curr.left!=null)dq.addLast(curr.left);if(curr.right!=null)dq.addLast(curr.right);}}reverse=!reverse;}returnres;}publicstaticvoidmain(String[]args){// Create a input binary tree// 20// / \// 8 22// / \ \// 4 12 11// / \// 10 14Noderoot=newNode(20);root.left=newNode(8);root.right=newNode(22);root.right.right=newNode(11);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);ArrayList<Integer>res=zigZagTraversal(root);for(intval:res)System.out.print(val+" ");System.out.println();}}
Python
fromcollectionsimportdeque# Node StructureclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=NonedefzigZagTraversal(root):res=[]ifnotroot:returnresdq=deque()dq.append(root)reverse=Falsewhiledq:n=len(dq)for_inrange(n):# Push right to the front # first if reverse is trueifreverse:curr=dq.pop()res.append(curr.data)ifcurr.right:dq.appendleft(curr.right)ifcurr.left:dq.appendleft(curr.left)# Else push left to the back firstelse:curr=dq.popleft()res.append(curr.data)ifcurr.left:dq.append(curr.left)ifcurr.right:dq.append(curr.right)reverse=notreversereturnresif__name__=="__main__":# Create a input binary tree# 20# / \# 8 22# / \ \# 4 12 11# / \# 10 14root=Node(20)root.left=Node(8)root.right=Node(22)root.right.right=Node(11)root.left.left=Node(4)root.left.right=Node(12)root.left.right.left=Node(10)root.left.right.right=Node(14)res=zigZagTraversal(root)forvalinres:print(val,end=" ")print()
C#
usingSystem;usingSystem.Collections.Generic;// Node StructureclassNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intx){data=x;left=null;right=null;}}classGFG{staticList<int>zigZagTraversal(Noderoot){List<int>res=newList<int>();if(root==null)returnres;LinkedList<Node>dq=newLinkedList<Node>();dq.AddLast(root);boolreverse=false;while(dq.Count>0){intn=dq.Count;while(n-->0){// Push right to the front // first if reverse is trueif(reverse){Nodecurr=dq.Last.Value;dq.RemoveLast();res.Add(curr.data);if(curr.right!=null)dq.AddFirst(curr.right);if(curr.left!=null)dq.AddFirst(curr.left);}// Else push left to the back firstelse{Nodecurr=dq.First.Value;dq.RemoveFirst();res.Add(curr.data);if(curr.left!=null)dq.AddLast(curr.left);if(curr.right!=null)dq.AddLast(curr.right);}}reverse=!reverse;}returnres;}staticvoidMain(){// Create a input binary tree// 20// / \// 8 22// / \ \// 4 12 11// / \// 10 14Noderoot=newNode(20);root.left=newNode(8);root.right=newNode(22);root.right.right=newNode(11);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);List<int>res=zigZagTraversal(root);foreach(intvalinres)Console.Write(val+" ");Console.WriteLine();}}
JavaScript
// Node StructureclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}classDeque{constructor(){this.front=[];this.back=[];}pushFront(val){this.front.push(val);}pushBack(val){this.back.push(val);}popFront(){if(this.front.length===0)this._balance('front');returnthis.front.pop();}popBack(){if(this.back.length===0)this._balance('back');returnthis.back.pop();}length(){returnthis.front.length+this.back.length;}_balance(side){if(side==='front'){while(this.back.length)this.front.push(this.back.pop());}else{while(this.front.length)this.back.push(this.front.pop());}}}functionzigZagTraversal(root){if(!root)return[];letres=[];letdq=newDeque();dq.pushBack(root);letreverse=false;while(dq.length()>0){letn=dq.length();for(leti=0;i<n;i++){// Push right to the front // first if reverse is trueif(reverse){letcurr=dq.popBack();res.push(curr.data);if(curr.right)dq.pushFront(curr.right);if(curr.left)dq.pushFront(curr.left);}// Else push left to the back firstelse{letcurr=dq.popFront();res.push(curr.data);if(curr.left)dq.pushBack(curr.left);if(curr.right)dq.pushBack(curr.right);}}reverse=!reverse;}returnres;}// Driver code// Create a input binary tree// 20// / \// 8 22// / \ \// 4 12 11// / \// 10 14letroot=newNode(20);root.left=newNode(8);root.right=newNode(22);root.right.right=newNode(11);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);letres=zigZagTraversal(root);console.log(res.join(" "));