Binary Tree to CDLL
Submissions: 2283   Accuracy:


  Difficulty: Medium   Marks: 4
Associated Course(s):   Interview Preparation

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.

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.

The function should return the head of CDLL.

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


1 2 R 1 3 L
10 20 L 10 30 R 20 40 L 20 60 R

3 1 2 
2 1 3
40 20 60 10 30 
30 10 60 20 40

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

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

to report an issue on this page.