Convert a Binary Tree into Doubly Linked List in spiral fashion
Last Updated : 23 Jul, 2025
Given a binary tree, convert it into a doubly linked list (DLL) where the nodes are arranged in a spiral order. The left pointer of the binary tree node should act as the previous node in the DLL, and the right pointer should act as the next node in the DLL.
The solution should not allocate extra memory for the DLL nodes. It should use the existing binary tree nodes for creating the DLL, meaning only changes to the pointers are allowed.
Example:
Input:
Output:
Explanation: The above binary tree is converted into doubly linked list where left pointer of the binary tree node act as the previous node and right pointer of the binary tree node act as the next node.
Approach:
The idea is to use a deque (double-ended queue) that can be expanded or contracted from both ends (either from its front or its back). We perform a level-order traversal, but to maintain spiral order, for every odd level, we dequeue a node from the front and insert its left and right children at the back of the deque. For each even level, we dequeue a node from the back and insert its right and left children at the front of the deque.
We also maintain a stack to store the binary tree nodes. Whenever we pop nodes from the deque, we push that node into the stack. Later, we pop all the nodes from the stack and insert them at the beginning of the list.
Below is the implementation of the above approach:
C++
// C++ program to convert Binary Tree into Doubly// Linked List where the nodes are represented spirally#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intval){data=val;left=right=nullptr;}};// Function to insert node at the // front of DLL and return the new headNode*push(Node*head,Node*node){node->right=head;node->left=nullptr;if(head!=nullptr){head->left=node;}returnnode;}// Function to perform spiral level-order traversal// and convert binary tree into DLLNode*spiralToDLL(Node*root){if(root==nullptr)returnnullptr;deque<Node*>dq;dq.push_front(root);stack<Node*>stk;Node*head=nullptr;boolleftToRight=true;// Perform spiral level order traversalwhile(!dq.empty()){intlevelSize=dq.size();while(levelSize--){if(leftToRight){// Left-to-right traversal: pop from frontNode*node=dq.front();dq.pop_front();// Push children for next level (left to right)if(node->left)dq.push_back(node->left);if(node->right)dq.push_back(node->right);stk.push(node);}else{// Right-to-left traversal: pop from backNode*node=dq.back();dq.pop_back();// Push children for next level (right to left)if(node->right)dq.push_front(node->right);if(node->left)dq.push_front(node->left);stk.push(node);}}leftToRight=!leftToRight;}// Pop nodes from stack and form the DLLwhile(!stk.empty()){Node*node=stk.top();stk.pop();// Insert node at the front of DLL and update headhead=push(head,node);}// Return the head of the created DLLreturnhead;}voidprintList(Node*node){while(node!=nullptr){cout<<node->data<<" ";node=node->right;}cout<<endl;}intmain(){// Example tree:// 1// / \ // 2 3// / \ / \ // 4 5 6 7Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);root->right->left=newNode(6);root->right->right=newNode(7);Node*head=spiralToDLL(root);printList(head);return0;}
Java
// Java program to convert Binary Tree into Doubly// Linked List where the nodes are represented spirallyimportjava.util.*;classNode{intdata;Nodeleft,right;Node(intval){data=val;left=right=null;}}// Function to insert node at the // front of DLL and return the new headclassGfG{staticNodepush(Nodehead,Nodenode){node.right=head;node.left=null;if(head!=null){head.left=node;}returnnode;}// Function to perform spiral level-order traversal// and convert binary tree into DLLstaticNodespiralToDLL(Noderoot){if(root==null)returnnull;Deque<Node>dq=newArrayDeque<>();dq.addFirst(root);Stack<Node>stk=newStack<>();Nodehead=null;booleanleftToRight=true;// Perform spiral level order traversalwhile(!dq.isEmpty()){intlevelSize=dq.size();while(levelSize-->0){if(leftToRight){// Left-to-right traversal: pop from frontNodenode=dq.pollFirst();// Push children for next level (left to right)if(node.left!=null)dq.addLast(node.left);if(node.right!=null)dq.addLast(node.right);stk.push(node);}else{// Right-to-left traversal: pop from backNodenode=dq.pollLast();// Push children for next level (right to left)if(node.right!=null)dq.addFirst(node.right);if(node.left!=null)dq.addFirst(node.left);stk.push(node);}}leftToRight=!leftToRight;}// Pop nodes from stack and form the DLLwhile(!stk.isEmpty()){Nodenode=stk.pop();// Insert node at the front of DLL and update headhead=push(head,node);}// Return the head of the created DLLreturnhead;}staticvoidprintList(Nodenode){while(node!=null){System.out.print(node.data+" ");node=node.right;}System.out.println();}publicstaticvoidmain(String[]args){// Example tree:// 1// / \// 2 3// / \ / \// 4 5 6 7Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);root.right.right=newNode(7);Nodehead=spiralToDLL(root);printList(head);}}
Python
# Python program to convert Binary Tree into Doubly# Linked List where the nodes are represented spirallyfromcollectionsimportdequeclassNode:def__init__(self,data):self.data=dataself.left=self.right=None# Function to insert node at the # front of DLL and return the new headdefpush(head,node):node.right=headnode.left=Noneifhead:head.left=nodereturnnode# Function to perform spiral level-order traversal# and convert binary tree into DLLdefspiralToDLL(root):ifnotroot:returnNonedq=deque([root])stk=[]head=NoneleftToRight=True# Perform spiral level order traversalwhiledq:levelSize=len(dq)for_inrange(levelSize):ifleftToRight:# Left-to-right traversal: pop from frontnode=dq.popleft()# Push children for next level (left to right)ifnode.left:dq.append(node.left)ifnode.right:dq.append(node.right)stk.append(node)else:# Right-to-left traversal: pop from backnode=dq.pop()# Push children for next level (right to left)ifnode.right:dq.appendleft(node.right)ifnode.left:dq.appendleft(node.left)stk.append(node)leftToRight=notleftToRight# Pop nodes from stack and form the DLLwhilestk:node=stk.pop()# Insert node at the front of DLL and update headhead=push(head,node)# Return the head of the created DLLreturnheaddefprintList(node):whilenode:print(node.data,end=" ")node=node.rightprint()if__name__=="__main__":# Example tree:# 1# / \# 2 3# / \ / \# 4 5 6 7root=Node(1)root.left=Node(2)root.right=Node(3)root.left.left=Node(4)root.left.right=Node(5)root.right.left=Node(6)root.right.right=Node(7)head=spiralToDLL(root)printList(head)
C#
// C# program to convert Binary Tree into Doubly// Linked List where the nodes are represented spirallyusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=right=null;}}classGfG{// Function to insert node at the // front of DLL and return the new headstaticNodePush(Nodehead,Nodenode){node.right=head;node.left=null;if(head!=null){head.left=node;}returnnode;}// Function to perform spiral level-order traversal// and convert binary tree into DLLstaticNodeSpiralToDLL(Noderoot){if(root==null)returnnull;LinkedList<Node>dq=newLinkedList<Node>();dq.AddFirst(root);Stack<Node>stk=newStack<Node>();Nodehead=null;boolleftToRight=true;// Perform spiral level order traversalwhile(dq.Count>0){intlevelSize=dq.Count;while(levelSize-->0){if(leftToRight){// Left-to-right traversal: pop from frontNodenode=dq.First.Value;dq.RemoveFirst();// Push children for next level (left to right)if(node.left!=null)dq.AddLast(node.left);if(node.right!=null)dq.AddLast(node.right);stk.Push(node);}else{// Right-to-left traversal: pop from backNodenode=dq.Last.Value;dq.RemoveLast();// Push children for next level (right to left)if(node.right!=null)dq.AddFirst(node.right);if(node.left!=null)dq.AddFirst(node.left);stk.Push(node);}}leftToRight=!leftToRight;}// Pop nodes from stack and form the DLLwhile(stk.Count>0){Nodenode=stk.Pop();// Insert node at the front of DLL and update headhead=Push(head,node);}// Return the head of the created DLLreturnhead;}staticvoidPrintList(Nodenode){while(node!=null){Console.Write(node.data+" ");node=node.right;}Console.WriteLine();}staticvoidMain(string[]args){// Example tree:// 1// / \// 2 3// / \ / \// 4 5 6 7Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);root.right.right=newNode(7);Nodehead=SpiralToDLL(root);PrintList(head);}}
JavaScript
// JavaScript program to convert Binary Tree into Doubly// Linked List where the nodes are represented spirallyclassNode{constructor(data){this.data=data;this.left=this.right=null;}}// Function to insert node at the // front of DLL and return the new headfunctionpush(head,node){node.right=head;node.left=null;if(head!==null){head.left=node;}returnnode;}// Function to perform spiral level-order traversal// and convert binary tree into DLLfunctionspiralToDLL(root){if(root===null)returnnull;letdq=[];dq.push(root);letstk=[];lethead=null;letleftToRight=true;// Perform spiral level order traversalwhile(dq.length>0){letlevelSize=dq.length;while(levelSize--){if(leftToRight){// Left-to-right traversal: pop from frontletnode=dq.shift();// Push children for next level (left to right)if(node.left!==null)dq.push(node.left);if(node.right!==null)dq.push(node.right);stk.push(node);}else{// Right-to-left traversal: pop from backletnode=dq.pop();// Push children for next level (right to left)if(node.right!==null)dq.unshift(node.right);if(node.left!==null)dq.unshift(node.left);stk.push(node);}}leftToRight=!leftToRight;}// Pop nodes from stack and form the DLLwhile(stk.length>0){letnode=stk.pop();// Insert node at the front of DLL and update headhead=push(head,node);}// Return the head of the created DLLreturnhead;}functionprintList(node){while(node!==null){console.log(node.data);node=node.right;}}// Example tree:// 1// / \// 2 3// / \ / \// 4 5 6 7letroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);root.right.right=newNode(7);lethead=spiralToDLL(root);printList(head);
Output
1 3 2 4 5 6 7
Time Complexity: O(n), as we are using a loop to traverse n times. Auxiliary Space: O(n), as we are using extra space for dequeue and stack.