Leaves to DLL
Submissions: 7053   Accuracy:

58.65%

  Difficulty: Medium   Marks: 4
Associated Course(s):   Sudo Placement 2019

Given a Binary Tree with N edges. The task is to extract all leaves of it in a Doubly Linked List (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 <= 103

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.

 

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