Open In App

POTD Solutions | 17 Nov’ 23 | Binary Tree to CDLL

Last Updated : 22 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

View all POTD Solutions

Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Binary Tree but will also help you build up problem-solving skills.

17th-nov

POTD Solution 17 November 2023

We recommend you to try this problem on our GeeksforGeeks Practice portal first, and maintain your streak to earn Geeksbits and other exciting prizes, before moving towards the solution.

POTD 17 November: Binary Tree to CDLL:

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

Examples:

Input:binaryTreeToLL

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.

Input:

binaryTreeToLL-(1)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.

Binary Tree to CDLL using Recursion:

The idea is to use Recursion and 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.

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:

C++




class Solution {
public:
    // 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 to convert binary tree into circular doubly
    // linked list.
    Node* bTreeToCList(Node* root)
    {
        // add code here.
        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);
    }
};


Java




class Solution
    // concatenate both the lists and returns the head 
    // of the List 
    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; 
    
    
    //Function to convert binary tree into circular doubly linked list.
    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); 
    }
      
}


Python3




class Solution:
  
    def concatenate(self, 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 to convert binary tree into circular doubly linked list.
    def bTreeToClist(self, root):
        # Your code here
        if (root == None):
            return None
  
        # Recursively convert left and
        # right subtrees
        left = self.bTreeToClist(root.left)
        right = self.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 self.concatenate(self.concatenate(left, root), right)


Time Complexity: O(N), where N is the number of nodes in the binary tree
Auxiliary Space: O(h), h is the height of the binary tree



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads