Binary Tree to CDLL | Practice | GeeksforGeeks

Given a Binary Tree of N edges. The task is to convert this to a Circular Doubly Linked List(CDLL) in-place. The left and right pointers in nodes are to be used as previous and next pointers respectively in converted CDLL. The order of nodes in CDLL must be same as Inorder of the given Binary Tree. The first node of Inorder traversal (left most node in BT) must be head node of the CDLL.
Example 1:
Input:
      1
    /   \
   3     2
Output:
3 1 2 
2 1 3
Explanation: After converting tree to CDLL
when CDLL is is traversed from head to
tail and then tail to head, elements
are displayed as in the output.

Example 2:
Input:
     10
   /    \
  20    30
 /  \
40  60
Output:
40 20 60 10 30 
30 10 60 20 40
Explanation:After converting tree to CDLL,
when CDLL is is traversed from head to
tail and then tail to head, elements
are displayed as in the output.
Your Task:
You don't have to take input. Complete the function bTreeToCList() that takes root as a parameter and returns the head of the list. The printing is done by the driver code. 
Constraints:
1 <= N <= 103
1 <= Data of a node <= 104
Expected time complexity: O(N)
Expected auxiliary space: O(h) , where h = height of tree
0
prateeks200056 minutes ago
class Solution{ private:   void solve(Node* root , Node* &head , Node* &tail){       if(!root) return;       solve(root->left,head,tail);       if(head==NULL){           head = tail = root;       }else{           tail->right = root;           root->left = tail;           tail = root;       }       solve(root->right,head,tail);   } public:   //Function to convert binary tree into circular doubly linked list.   Node *bTreeToCList(Node *root)   {   //add code here.       Node* head = NULL , *tail=NULL;       solve(root,head,tail);       if(head!=NULL){           head->left = tail;           tail->right = head;       }       return head;   }};
0
tthakare731 day ago
//java solution -> TC -> 1.72
class Solution{
    static ArrayList<Node> adj;
    void triv(Node root){
        if(root == null) return;
        triv(root.left);
        adj.add(root);
        triv(root.right);
    }
    
    Node bTreeToClist(Node root){
        adj = new ArrayList<>();
        triv(root);
        
        Node Result = null, dummy = null;
        for(int i = 0; i < adj.size(); i++){
            Node newNode  = adj.get(i);
            
            if(Result == null){
                Result = newNode;
                dummy = newNode;
            } else {
                dummy.right = newNode;
                newNode.left = dummy;
                dummy = dummy.right;
            }
            if(i == adj.size() - 1){
                 Result.left = dummy;
                 dummy.right = Result;
            }
        }
        return Result;
    }
}
0
rmn51242 days ago
class Solution
{
  public:
    //Function to convert binary tree into circular doubly linked list.
    void inorder(Node*root,Node* &head,Node* &prev,int& f){
        if(!root) return;
        inorder(root->left,head,prev,f);
        if(f==0){
            head=root;
            prev=head;
            f=1;
        }
        else{
            prev->right=root;
              prev->right->left=prev;
            prev=prev->right;
             
        }
         inorder(root->right,head,prev,f);
         // only diff is that connect last to head
         prev->right=head;
         head->left=prev;
    }
    
    Node *bTreeToCList(Node *root)
    {
        int f=0;
        Node*head=NULL;
        Node*prev=NULL;
        inorder(root,head,prev,f);
        return head;
    
    }
};

+1
triloki351 week ago
Node *prev = NULL;    Node *head = NULL;   Node *bTreeToCList(Node *root)   {       if(root==NULL)       return NULL;              bTreeToCList(root->left);              if(prev==NULL)       {           head = root;       }       else       {           prev->right = root;           root->left = prev;           head->left = root;       }              prev = root;              Node* temp = bTreeToCList(root->right);              if(temp==NULL)       root->right = head;              return head;   }
0
subhamk472 weeks ago
class Solution{    Node prev = null, tail = null;   Node bTreeToClist(Node root)   {       Node res = convert(root);       tail.right=res;       res.left=tail;       return res;   }      Node convert(Node root){       if(root==null) {           return root;       }       Node head = convert(root.left);       if(prev == null) head = root;       else{           prev.right = root;           root.left = prev;       }       prev = root;       convert(root.right);       if (root.right==null) tail = root;        return head;   }   }
-1
cs200152 weeks ago
class Solution{ public:   //Function to convert binary tree into circular doubly linked list.   Node *bTreeToCList(Node *root)   {           struct Node*head=NULL;       struct Node*pre=NULL;               if(!root)        return NULL;        bTreeToCList(root->left);        if(!pre){        head=root;   }   else{       pre->right=root;       root->left=pre;   } pre=root;    bTreeToCList(root->right);    pre->right=head;    head->left=pre;         return head;}};
 
 
 
0
crawler1 month ago
	void add(Node *&head, Node *&tail, int dat){
        if(!head || !tail){
            Node *newnode = new Node();
            newnode->data = dat;
            head = tail = newnode;
            tail->right = head;
            head->left = tail;
            return;
        }
        else{
            Node *newnode = new Node();
            newnode->data = dat;
            newnode->left = tail;
            tail->right=newnode;
            head->left = newnode;
            newnode->right = head;
            tail = newnode;
        }
    }
    void inorder(Node *root, Node *&head, Node *&tail){
        if(root){
            inorder(root->left, head, tail);
            add(head, tail, root->data);
            inorder(root->right, head, tail);
        }
    }
    Node *bTreeToCList(Node *root)
    {
        if(!root)
            return nullptr;
        Node *head = nullptr;
        Node *tail = nullptr;
        inorder(root, head, tail);
        
        return head;
    }
+1
shirurrohit1 month ago
PYTHON - EASY - DLL
 
class Solution:
    
    def __init__(self):
        self.prev = None
        self.head = None
        
    def helper(self, root):
        if root == None:
            return
        
        self.helper(root.left)
        
        if self.head == None:
            self.head = root
        else:
            self.prev.right = root
            root.left = self.prev
        self.prev = root
        
        self.helper(root.right)
    
    def bTreeToClist(self, root):
        self.helper(root)
        self.prev.right = self.head
        self.head.left = self.prev
        return self.head
0
psadv1 month ago
For this problem>   we can reduce the complexity of CDLL to just DLL  step1: just build DLL during inorder traversal  step2: after inorder completion, set tail.right = head and head.left = tail
0
priyeshanand92 months ago
class Solution{ public:   //Function to convert binary tree into circular doubly linked list.   void convert(Node *&prev, Node *&head, Node *root)   {       if(root==NULL)       return;       convert(prev,head,root->left);       if(prev==NULL){           head=root;           prev=root;       }       else{           prev->right=root;           prev->right->left=prev;           prev=root;       }       convert(prev,head,root->right);   }   Node *bTreeToCList(Node *root)   {    Node *prev=NULL;    Node *head=NULL;    convert(prev,head,root);    Node *p=head;    prev->right=NULL;    while(p){        if(p->right==NULL){            p->right=head;            head->left=p;            break;        }        p=p->right;    }    return head;   }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.