Leaves to DLL
Submissions: 3834   Accuracy:


  Difficulty: Medium   Marks: 4

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.

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.

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.

1 <= T <= 100
1 <= N <= 103


1 2 L 1 3 R
1 2 L 1 3 R 2 4 L

2 3
3 2 
4 3
3 4

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

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

to report an issue on this page.