Convert a Binary Tree to a Circular Doubly Link List
Given a Binary Tree, convert it to a Circular Doubly Linked List (In-Place).
- The left and right pointers in nodes are to be used as previous and next pointers respectively in converted Circular Linked List.
- The order of nodes in the List must be the same as in Inorder for the given Binary Tree.
- The first node of Inorder traversal must be the head node of the Circular List.
Example:

The idea can be described using the below steps.
1) Write a general-purpose function that concatenates two given circular doubly lists (This function is explained below).
2) Now traverse the given tree
….a) Recursively convert left subtree to a circular DLL. Let the converted list be leftList.
….a) Recursively convert right subtree to a circular DLL. Let the converted list be rightList.
….c) Make a circular linked list of root of the tree, make left and right of root to point to itself.
….d) Concatenate leftList with the list of the single root node.
….e) Concatenate the list produced in the step above (d) with rightList.
Note that the above code traverses the tree in Postorder fashion. We can traverse in an inorder fashion also. We can first concatenate left subtree and root, then recur for the right subtree and concatenate the result with left-root concatenation.
How to Concatenate two circular DLLs?
- Get the last node of the left list. Retrieving the last node is an O(1) operation since the prev pointer of the head points to the last node of the list.
- Connect it with the first node of the right list
- Get the last node of the second list
- Connect it with the head of the list.
Below are implementations of the above idea.
C++
// C++ Program to convert a Binary Tree// to a Circular Doubly Linked List#include<iostream>using namespace std; // To represents a node of a Binary Treestruct Node{ struct Node *left, *right; int data;}; // A function that appends rightList at the end// of leftList.Node *concatenate(Node *leftList, Node *rightList){ // If either of the list is empty // then return the other list if (leftList == NULL) return rightList; if (rightList == NULL) return leftList; // Store the last Node of left List Node *leftLast = leftList->left; // Store the last Node of right List Node *rightLast = rightList->left; // Connect the last node of Left List // with the first Node of the right List leftLast->right = rightList; rightList->left = leftLast; // Left of first node points to // the last node in the list leftList->left = rightLast; // Right of last node refers to the first // node of the List rightLast->right = leftList; return leftList;} // Function converts a tree to a circular Linked List// and then returns the head of the Linked ListNode *bTreeToCList(Node *root){ if (root == NULL) return NULL; // Recursively convert left and right subtrees Node *left = bTreeToCList(root->left); Node *right = bTreeToCList(root->right); // Make a circular linked list of single node // (or root). To do so, make the right and // left pointers of this node point to itself root->left = root->right = root; // Step 1 (concatenate the left list with the list // with single node, i.e., current node) // Step 2 (concatenate the returned list with the // right List) return concatenate(concatenate(left, root), right);} // Display Circular Link Listvoid displayCList(Node *head){ cout << "Circular Linked List is :\n"; Node *itr = head; do { cout << itr->data <<" "; itr = itr->right; } while (head!=itr); cout << "\n";} // Create a new Node and return its addressNode *newNode(int data){ Node *temp = new Node(); temp->data = data; temp->left = temp->right = NULL; return temp;} // Driver Program to test above functionint main(){ Node *root = newNode(10); root->left = newNode(12); root->right = newNode(15); root->left->left = newNode(25); root->left->right = newNode(30); root->right->left = newNode(36); Node *head = bTreeToCList(root); displayCList(head); return 0;} |
Java
// Java Program to convert a Binary Tree to a// Circular Doubly Linked List // Node class represents a Node of a Treeclass Node{ int val; Node left,right; public Node(int val) { this.val = val; left = right = null; }} // A class to represent a treeclass Tree{ Node root; public Tree() { root = null; } // concatenate both the lists and returns the head // of the List public Node concatenate(Node leftList,Node rightList) { // If either of the list is empty, then // return the other list if (leftList == null) return rightList; if (rightList == null) return leftList; // Store the last Node of left List Node leftLast = leftList.left; // Store the last Node of right List Node rightLast = rightList.left; // Connect the last node of Left List // with the first Node of the right List leftLast.right = rightList; rightList.left = leftLast; // left of first node refers to // the last node in the list leftList.left = rightLast; // Right of last node refers to the first // node of the List rightLast.right = leftList; // Return the Head of the List return leftList; } // Method converts a tree to a circular // Link List and then returns the head // of the Link List public Node bTreeToCList(Node root) { if (root == null) return null; // Recursively convert left and right subtrees Node left = bTreeToCList(root.left); Node right = bTreeToCList(root.right); // Make a circular linked list of single node // (or root). To do so, make the right and // left pointers of this node point to itself root.left = root.right = root; // Step 1 (concatenate the left list with the list // with single node, i.e., current node) // Step 2 (concatenate the returned list with the // right List) return concatenate(concatenate(left, root), right); } // Display Circular Link List public void display(Node head) { System.out.println("Circular Linked List is :"); Node itr = head; do { System.out.print(itr.val+ " " ); itr = itr.right; } while (itr != head); System.out.println(); }} // Driver Codeclass Main{ public static void main(String args[]) { // Build the tree Tree tree = new Tree(); tree.root = new Node(10); tree.root.left = new Node(12); tree.root.right = new Node(15); tree.root.left.left = new Node(25); tree.root.left.right = new Node(30); tree.root.right.left = new Node(36); // head refers to the head of the Link List Node head = tree.bTreeToCList(tree.root); // Display the Circular LinkedList tree.display(head); }} |
Python3
# Python3 Program to convert a Binary # Tree to a Circular Doubly Linked List class newNode: def __init__(self, data): self.data = data self.left = self.right = None # A function that appends rightList # at the end of leftList. def concatenate(leftList, rightList): # If either of the list is empty # then return the other list if (leftList == None): return rightList if (rightList == None): return leftList # Store the last Node of left List leftLast = leftList.left # Store the last Node of right List rightLast = rightList.left # Connect the last node of Left List # with the first Node of the right List leftLast.right = rightList rightList.left = leftLast # Left of first node points to # the last node in the list leftList.left = rightLast # Right of last node refers to # the first node of the List rightLast.right = leftList return leftList # Function converts a tree to a circular # Linked List and then returns the head # of the Linked List def bTreeToCList(root): if (root == None): return None # Recursively convert left and # right subtrees left = bTreeToCList(root.left) right = bTreeToCList(root.right) # Make a circular linked list of single # node (or root). To do so, make the # right and left pointers of this node # point to itself root.left = root.right = root # Step 1 (concatenate the left list # with the list with single # node, i.e., current node) # Step 2 (concatenate the returned list # with the right List) return concatenate(concatenate(left, root), right) # Display Circular Link List def displayCList(head): print("Circular Linked List is :") itr = head first = 1 while (head != itr or first): print(itr.data, end = " ") itr = itr.right first = 0 print() # Driver Codeif __name__ == '__main__': root = newNode(10) root.left = newNode(12) root.right = newNode(15) root.left.left = newNode(25) root.left.right = newNode(30) root.right.left = newNode(36) head = bTreeToCList(root) displayCList(head) # This code is contributed by PranchalK |
C#
// C# Program to convert a Binary Tree // to a Circular Doubly Linked List using System; // Node class represents a Node of a Tree public class Node{ public int val; public Node left, right; public Node(int val) { this.val = val; left = right = null; }} // A class to represent a tree public class Tree{ internal Node root; public Tree() { root = null; } // concatenate both the lists // and returns the head of the List public virtual Node concatenate(Node leftList, Node rightList) { // If either of the list is empty, // then return the other list if (leftList == null) { return rightList; } if (rightList == null) { return leftList; } // Store the last Node of left List Node leftLast = leftList.left; // Store the last Node of right List Node rightLast = rightList.left; // Connect the last node of Left List // with the first Node of the right List leftLast.right = rightList; rightList.left = leftLast; // left of first node refers to // the last node in the list leftList.left = rightLast; // Right of last node refers to // the first node of the List rightLast.right = leftList; // Return the Head of the List return leftList; } // Method converts a tree to a circular // Link List and then returns the head // of the Link List public virtual Node bTreeToCList(Node root) { if (root == null) { return null; } // Recursively convert left // and right subtrees Node left = bTreeToCList(root.left); Node right = bTreeToCList(root.right); // Make a circular linked list of single // node (or root). To do so, make the // right and left pointers of this node // point to itself root.left = root.right = root; // Step 1 (concatenate the left list with // the list with single node, // i.e., current node) // Step 2 (concatenate the returned list // with the right List) return concatenate(concatenate(left, root), right); } // Display Circular Link List public virtual void display(Node head) { Console.WriteLine("Circular Linked List is :"); Node itr = head; do { Console.Write(itr.val + " "); itr = itr.right; } while (itr != head); Console.WriteLine(); }} // Driver Code public class GFG{ public static void Main(string[] args) { // Build the tree Tree tree = new Tree(); tree.root = new Node(10); tree.root.left = new Node(12); tree.root.right = new Node(15); tree.root.left.left = new Node(25); tree.root.left.right = new Node(30); tree.root.right.left = new Node(36); // head refers to the head of the Link List Node head = tree.bTreeToCList(tree.root); // Display the Circular LinkedList tree.display(head); }} // This code is contributed by Shrikant13 |
Javascript
<script>// javascript Program to convert a Binary Tree to a// Circular Doubly Linked List // Node class represents a Node of a Treeclass Node { constructor(val) { this.val = val; this.left = null; this.right = null; }} // A class to represent a var root = null; // concatenate both the lists and returns the head // of the List function concatenate(leftList, rightList) { // If either of the list is empty, then // return the other list if (leftList == null) return rightList; if (rightList == null) return leftList; // Store the last Node of left List var leftLast = leftList.left; // Store the last Node of right List var rightLast = rightList.left; // Connect the last node of Left List // with the first Node of the right List leftLast.right = rightList; rightList.left = leftLast; // left of first node refers to // the last node in the list leftList.left = rightLast; // Right of last node refers to the first // node of the List rightLast.right = leftList; // Return the Head of the List return leftList; } // Method converts a to a circular // Link List and then returns the head // of the Link List function bTreeToCList(root) { if (root == null) return null; // Recursively convert left and right subtrees var left = bTreeToCList(root.left); var right = bTreeToCList(root.right); // Make a circular linked list of single node // (or root). To do so, make the right and // left pointers of this node point to itself root.left = root.right = root; // Step 1 (concatenate the left list with the list // with single node, i.e., current node) // Step 2 (concatenate the returned list with the // right List) return concatenate(concatenate(left, root), right); } // Display Circular Link List function display(head) { document.write("Circular Linked List is :<br/>"); var itr = head; do { document.write(itr.val + " "); itr = itr.right; } while (itr != head); document.write(); } // Driver Code // Build the 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); // head refers to the head of the Link List var head = bTreeToCList(root); // Display the Circular LinkedList display(head); // This code contributed by umadevi9616</script> |
Output:
Circular Linked List is : 25 12 30 10 36 15
This article is contributed by Chirag Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


