Convert Binary Tree to Circular Doubly Linked List using Linear extra space
Given a Binary Tree, convert it to a Circular Doubly Linked List.
- The left and right pointers in nodes are to be used as previous and next pointers, respectively, in the converted Circular Linked List.
- The order of nodes in the List must be the same as in the order of the given Binary Tree.
- The first node of Inorder traversal must be the head node of the Circular List.
Example:
An in-place solution to this problem is discussed in the previous post.
In this post, a much simpler solution, but using extra O(n) space, is discussed.
In this approach, first, we do an In-Order traversal of the given binary tree, we store this traversal in a vector, which will be passed on to the function along with the tree. Now, generate a circular doubly linked list from the elements of the vector.
To generate a circular doubly linked list from the vector, make the first element of the vector as the head of the linked list and also create a current pointer that is right now pointing to the head. Now start traversing the array from the second element and do the following.
- Create a temporary pointer that points to the current pointer.
- Make a new node with the current element of the vector.
- Make the current right point to this new node.
- Make the current pointer the current right pointer.
- Now, finally, the current left point is a temporary pointer created in the first step.
Do this for all the elements and after the traversal is completed, make the current’s right (current is right now pointing to the last element of the vector) point to the head of the list and make the head’s left point to the current pointer. Finally, return the head.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
Node( int x)
{
data = x;
left = right = NULL;
}
};
void inorder(Node* root, vector< int >& v)
{
if (!root)
return ;
inorder(root->left, v);
v.push_back(root->data);
inorder(root->right, v);
}
Node* bTreeToCList(Node* root)
{
if (root == NULL)
return NULL;
vector< int > v;
inorder(root, v);
Node* head_ref = new Node(v[0]);
Node* curr = head_ref;
for ( int i = 1; i < v.size(); i++) {
Node* temp = curr;
curr->right = new Node(v[i]);
curr = curr->right;
curr->left = temp;
}
curr->right = head_ref;
head_ref->left = curr;
return head_ref;
}
void displayCList(Node* head)
{
cout << "Circular Doubly Linked List is :\n" ;
Node* itr = head;
do {
cout << itr->data << " " ;
itr = itr->right;
} while (head != itr);
cout << "\n" ;
}
int main()
{
Node* root = new Node(10);
root->left = new Node(12);
root->right = new Node(15);
root->left->left = new Node(25);
root->left->right = new Node(30);
root->right->left = new Node(36);
Node* head = bTreeToCList(root);
displayCList(head);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node left;
Node right;
Node( int x)
{
data = x;
left = right = null ;
}
};
static void inorder(Node root, Vector<Integer> v)
{
if (root == null )
return ;
inorder(root.left, v);
v.add(root.data);
inorder(root.right, v);
}
static Node bTreeToCList(Node root)
{
if (root == null )
return null ;
Vector<Integer> v = new Vector<>();
inorder(root, v);
Node head_ref = new Node(v.get( 0 ));
Node curr = head_ref;
for ( int i = 1 ; i < v.size(); i++)
{
Node temp = curr;
curr.right = new Node(v.get(i));
curr = curr.right;
curr.left = temp;
}
curr.right = head_ref;
head_ref.left = curr;
return head_ref;
}
static void displayCList(Node head)
{
System.out.println( "Circular Doubly Linked List is :" );
Node itr = head;
do
{
System.out.print(itr.data + " " );
itr = itr.right;
} while (head != itr);
System.out.println();
}
public static void main(String[] args)
{
Node root = new Node( 10 );
root.left = new Node( 12 );
root.right = new Node( 15 );
root.left.left = new Node( 25 );
root.left.right = new Node( 30 );
root.right.left = new Node( 36 );
Node head = bTreeToCList(root);
displayCList(head);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self .left = self .right = None
v = []
def inorder(root):
global v
if (root = = None ):
return
inorder(root.left)
v.append(root.data)
inorder(root.right)
def bTreeToCList(root):
global v
if (root = = None ):
return None
v = []
inorder(root)
head_ref = Node(v[ 0 ])
curr = head_ref
i = 1
while ( i < len (v)) :
temp = curr
curr.right = Node(v[i])
curr = curr.right
curr.left = temp
i = i + 1
curr.right = head_ref
head_ref.left = curr
return head_ref
def displayCList(head):
print ( "Circular Doubly Linked List is :" , end = "")
itr = head
while ( True ):
print (itr.data, end = " " )
itr = itr.right
if (head = = itr):
break
print ()
root = Node( 10 )
root.left = Node( 12 )
root.right = Node( 15 )
root.left.left = Node( 25 )
root.left.right = Node( 30 )
root.right.left = Node( 36 )
head = bTreeToCList(root)
displayCList(head)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node left;
public Node right;
public Node( int x)
{
data = x;
left = right = null ;
}
};
static void inorder(Node root, List< int > v)
{
if (root == null )
return ;
inorder(root.left, v);
v.Add(root.data);
inorder(root.right, v);
}
static Node bTreeToCList(Node root)
{
if (root == null )
return null ;
List< int > v = new List< int >();
inorder(root, v);
Node head_ref = new Node(v[0]);
Node curr = head_ref;
for ( int i = 1; i < v.Count; i++)
{
Node temp = curr;
curr.right = new Node(v[i]);
curr = curr.right;
curr.left = temp;
}
curr.right = head_ref;
head_ref.left = curr;
return head_ref;
}
static void displayCList(Node head)
{
Console.WriteLine( "Circular Doubly " +
"Linked List is :" );
Node itr = head;
do
{
Console.Write(itr.data + " " );
itr = itr.right;
} while (head != itr);
Console.WriteLine();
}
public static void Main(String[] args)
{
Node root = new Node(10);
root.left = new Node(12);
root.right = new Node(15);
root.left.left = new Node(25);
root.left.right = new Node(30);
root.right.left = new Node(36);
Node head = bTreeToCList(root);
displayCList(head);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
function inorder(root, v) {
if (root == null )
return ;
inorder(root.left, v);
v.push(root.data);
inorder(root.right, v);
}
function bTreeToCList(root) {
if (root == null )
return null ;
var v = [];
inorder(root, v);
var head_ref = new Node(v[0]);
var curr = head_ref;
for (i = 1; i < v.length; i++) {
var temp = curr;
curr.right = new Node(v[i]);
curr = curr.right;
curr.left = temp;
}
curr.right = head_ref;
head_ref.left = curr;
return head_ref;
}
function displayCList(head) {
document.write( "Circular Doubly Linked List is :<br/>" );
var itr = head;
do {
document.write(itr.data + " " );
itr = itr.right;
} while (head != itr);
document.write();
}
var root = new Node(10);
root.left = new Node(12);
root.right = new Node(15);
root.left.left = new Node(25);
root.left.right = new Node(30);
root.right.left = new Node(36);
var head = bTreeToCList(root);
displayCList(head);
</script>
|
Output:
Circular Doubly Linked List is :
25 12 30 10 36 15
Time Complexity: O(N), where N is the number of nodes in the Binary Tree.
Auxiliary Space: O(N)
Last Updated :
28 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...