Binary Tree to CDLL
Submissions: 4637   Accuracy:

59.16%

  Difficulty: Medium   Marks: 4

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.

Input Format:
First line of input contains number of testcases T. For each testcase, there will be two lines, first of which containing the number of edges (between two nodes) in the tree. Next line contains N pairs (considering a and b) with a 'L' (means node b on left of a) or 'R' (means node b on right of a) after a and b

Output Format:
For each testcase, in a new line, print the traversals of CDLL.

Your Task:
You don't have to take input. Complete the function bTreeToCList() that takes root as parameter and returns the head of the list. The printing is done by the driver code.

Constraints:
1 <= T <= 100
1 <= N <= 103
1 <= Data of a node <= 104

Example:
Input:

2
2
1 2 R 1 3 L
4
10 20 L 10 30 R 20 40 L 20 60 R
Output:
3 1 2 
2 1 3
40 20 60 10 30 
30 10 60 20 40

Explanation:
Testcase 1:
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.

 

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Saksham Raj Seth
Author: saksham seth


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.