题目描述
Given a Binary Tree of size N, extract all its leaf nodes to form a Doubly Link List starting 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.
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.
Example 1:
Input :
1
/ \
2 3
/ \ / \
4 5 6 7
Output:
Modified Tree :
1
/ \
2 3
Doubly Link List :
4 <-> 5 <-> 6 <-> 7
Explanation:
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 5
Output:
Modified Tree :
1
/
2
Doubly Link List :
4 <-> 5 <-> 3
Your Task:
You don't need to read input or print anything. Complete the function convertToDLL() which takes the root of the given tree as input parameter and returns the head of the doubly link list.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(height of tree)
Constraints:
1 ≤ N ≤ 10^4
输入格式
见题面
输出格式
见题面