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.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

devsinghindra | 80 |

PiyushPandey4 | 614 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 443 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.