Convert a given Binary Tree to Circular Doubly Linked List | Set 2 | A C ++ program for conversion of Binary Tree to CDLL ; A binary tree node has data , and left and right pointers ; Function to perform In - Order traversal of the tree and store the nodes in a vector ; first recur on left child ; append the data of node in vector ; now recur on right child ; Function to convert Binary Tree to Circular Doubly Linked list using the vector which stores In - Order traversal of the Binary Tree ; Base cases ; Vector to be used for storing the nodes of tree in In - order form ; Calling the In - Order traversal function ; Create the head of the linked list pointing to the root of the tree ; Create a current pointer to be used in traversal ; Traversing the nodes of the tree starting from the second elements ; Create a temporary pointer pointing to current ; Current 's right points to the current  node in traversal ; Current points to its right ; Current 's left points to temp ; Current 's right points to head of the list ; Head 's left points to current ; Return head of the list ; Display Circular Link List ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ▁ : STRNEWLINE " ; Node * itr = head ; do { cout << itr -> data << " ▁ " ; itr = itr -> right ; } while ( head != itr ) ; cout << " STRNEWLINE " ; } 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 ; }