![]() |
VOOZH | about |
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.
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 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.
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:
How do Concatenate two circular DLLs?
Below are implementations of the above idea:
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