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 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 40Explanation: 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:
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);
}
}; |
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);
}
} |
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