Geeksforgeeks

Timer is Running

Given a Binary Tree of size N, extract all its leaf nodes to form a Doubly Link List strating from the left most leaf. Modify the original tree to make the DLL thus removing the leaf nodes from the tree. Consider the left and right pointers of the tree to be the previous and next pointer of the DLL respectively.

**Example 1:**

Input :1 / \ 2 3 / \ / \ 4 5 6 7Output:Modified Tree : 1 / \ 2 3 Doubly Link List : 4 <-> 5 <-> 6 <-> 7Explanation:The leaf nodes are modified to form the DLL in-place. Thus their links are removed from the tree.

**Example 2:**

Input :1 / \ 2 3 / \ 4 5Output:Modified Tree : 1 / 2 Doubly Link List : 4 <-> 5 <-> 3

**Your Task: **

You dont need to read input or print anything. Complete the function **convertToDLL()** which takes root of the given tree as input parameter and returns the head of the doubly link list.

**Note:**

The generated output will contain the inorder traversal of the modified tree, the DLL from left to right and the DLL from right to left.

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(height of tree)

**Constraints:**

1 ≤ N ≤ 10^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.

jainshubham52535 | 529 |

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

jots62 | 351 |

kirtidee18 | 1057 |

ykaushik | 752 |

aggarwalvatsal5 | 741 |

harbilassingh | 576 |

pmr_3689 | 570 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4827 |

Login to report an issue on this page.