POTD Solutions | 17 Nov’ 23 | Binary Tree to CDLL
Last Updated :
22 Nov, 2023
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.
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:
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:
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.
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 :
Node* concatenate(Node* leftList, Node* rightList)
{
if (leftList == NULL)
return rightList;
if (rightList == NULL)
return leftList;
Node* leftLast = leftList->left;
Node* rightLast = rightList->left;
leftLast->right = rightList;
rightList->left = leftLast;
leftList->left = rightLast;
rightLast->right = leftList;
return leftList;
}
Node* bTreeToCList(Node* root)
{
if (root == NULL)
return NULL;
Node* left = bTreeToCList(root->left);
Node* right = bTreeToCList(root->right);
root->left = root->right = root;
return concatenate(concatenate(left, root), right);
}
};
|
Java
class Solution
{
Node concatenate(Node leftList, Node rightList)
{
if (leftList == null )
return rightList;
if (rightList == null )
return leftList;
Node leftLast = leftList.left;
Node rightLast = rightList.left;
leftLast.right = rightList;
rightList.left = leftLast;
leftList.left = rightLast;
rightLast.right = leftList;
return leftList;
}
Node bTreeToClist(Node root)
{
if (root == null )
return null ;
Node left= bTreeToClist(root.left);
Node right = bTreeToClist(root.right);
root.left = root.right = root;
return concatenate(concatenate(left, root), right);
}
}
|
Python3
class Solution:
def concatenate( self , leftList, rightList):
if (leftList = = None ):
return rightList
if (rightList = = None ):
return leftList
leftLast = leftList.left
rightLast = rightList.left
leftLast.right = rightList
rightList.left = leftLast
leftList.left = rightLast
rightLast.right = leftList
return leftList
def bTreeToClist( self , root):
if (root = = None ):
return None
left = self .bTreeToClist(root.left)
right = self .bTreeToClist(root.right)
root.left = root.right = root
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
Share your thoughts in the comments
Please Login to comment...