Given a Binary Tree with **N** edges. The task is to extract all leaves of it in a **D**oubly **L**inked **L**ist (DLL). Note that the DLL need to be created in-place. Assume that the node structure of DLL and Binary Tree is same, only the meaning of left and right pointers are different. In DLL, left means previous pointer and right means next pointer. Head of DLL should point to the left most leaf and then in inorder traversal and so on.

**Input:**

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:**

For each testcase, there will be two lines containing the nodes of DLL, first in reverse order and next in order of inorder traversal of tree.

**User Task:**

The task is to complete the function **convertToDLL**() which converts given binary tree to DLL.

**Constraints:**

1 <= T <= 100

1 <= N <= 10^{3}

**Example:
Input:**

2

2

1 2 L 1 3 R

3

1 2 L 1 3 R 2 4 L

**Output:**

2 3

3 2

4 3

3 4

**Explanation:
Testcase 2:** After extracting leaves, 3 and 4 from the tree, we have doubly linked list as 3 <-> 4.

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.

Vagesh Verma | 166 |

vamsikrishna55 | 162 |

UrGuru | 150 |

abducodes | 145 |

adithya_99 | 119 |

abducodes | 701 |

harshtyagi36 | 326 |

sandipbhaumik | 291 |

Akhil varma | 267 |

arpit_anshuman | 262 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.