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 converted Circular Linked List.
- The order of nodes in List must be same as In order of the given Binary Tree.
- The first node of Inorder traversal must be the head node of the Circular List.
An in-place solution to this problem is discussed in previous post.
In this post, a much simpler solution, but using extra O(n) space, is discussed.
In this approach, firstly we will 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 which is right now pointing to head. Now start traversing the array from the second element and do the following.
- Create a temporary pointer which points to the current pointer.
- Make a new node with the current element of the vector.
- Make current’s right point to this new node.
- Make current pointer equals to current’s right pointer.
- Now finally make the current’s left point to temporary pointer created in the first step.
Do this for all the elements and after the traversal is completed, make current’s right (current is right now pointing to 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:
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)
- Convert a Binary Tree to a Circular Doubly Link List
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- Convert an Array to a Circular Doubly Linked List
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Reverse a doubly circular linked list
- Doubly Circular Linked List | Set 2 (Deletion)
- Search an Element in Doubly Circular Linked List
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Insertion at Specific Position in a Circular Doubly Linked List
- Convert singly linked list into circular linked list
- Create a Doubly Linked List from a Ternary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.