Open in App
Not now

# Convert a Binary Tree to a Circular Doubly Link List

• Difficulty Level : Hard
• Last Updated : 10 Jan, 2023

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 the 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.

Examples:

Recommended Practice

## Convert a Binary Tree to a Circular Doubly Link List using Recursion:

The idea is to make a general-purpose function that concatenates two given circular doubly lists

Follow the steps below to solve the problem:

• Recursively convert the left subtree to a circular DLL. Let the converted list be leftList.
• Recursively convert the right subtree to a circular DLL. Let the converted list be rightList
• Make a circular linked list of roots of the tree, and make the left and right root points to themselves.
• Concatenate leftList with the list of the single root node.
• Concatenate the list produced in the step above with rightList.

Note: The above approach traverses the tree in a 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 do 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:

## Javascript



Output

25 12 30 10 36 15

Time Complexity: O(N), As every node is visited at most once.
Auxiliary space: O(log N), The extra space is used in the recursion call stack which can grow up to a maximum size of logN as it is a binary tree.

## Convert a Binary Tree to a Circular Doubly Link List by Inorder Traversal:

The idea is to do in-order traversal of the binary tree. While doing inorder traversal, keep track of the previously visited node in a variable, say prev. For every visited node, make it the next of the prev and set previous of this node as prev.

Follow the steps below to solve the problem:

Below is the implementation of the above approach.

## Javascript

Output

25 12 30 10 36 15

Time Complexity: O(N), As every node is visited at most once.
Auxiliary space: O(log N), The extra space is used in the recursive function call stack which can grow upto a maximum size of logN.

This approach was contributed by Abhijeet Kumar

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.

My Personal Notes arrow_drop_up
Related Articles