You are given a binary tree consisting of 'n' nodes.
Convert the given binary tree into a linked list where the linked list nodes follow the same order as the pre-order traversal of the given binary tree.
Use the right pointer of the binary tree as the “next” pointer for the linked list and set the left pointer to NULL.
Use these nodes only. Do not create extra nodes.
Input: Let the binary be as shown in the figure:
Output: Linked List: 15 -> 40 -> 62 -> 10 -> 20 -> NULL
Explanation: As shown in the figure, the right child of every node points to the next node, while the left node points to null.
Also, the nodes are in the same order as the pre-order traversal of the binary tree.
The first line of input contains elements in the level order form for the first binary tree. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
The level order input for the tree depicted in the below image would be

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level, and so on.
The input ends when all nodes at the last level are null (-1).
The nodes of the linked list, each separated by a single space, are printed.
You do not need to print anything; it has already been taken care of. Just implement the given function.
15 40 62 -1 -1 10 20 -1 -1 -1 -1
15 40 62 10 20
The binary tree and the corresponding linked list are given in the following figure:
As shown in the figure, the right child of every node points to the next node, while the left node points to null.
Also, the nodes are in the same order as the pre-order traversal of the binary tree.
1 2 3 -1 -1 4 -1 5 -1 -1 -1
1 2 3 4 5
The binary tree and the corresponding linked list are given in the following figure:
As shown in the figure, the right child of every node points to the next node, while the left node points to null.
Also, the nodes are in the same order as the pre-order traversal of the binary tree, that is [1, 2, 3, 4, 5].
The expected time complexity is O(n).
1 <= 'n' <= 10 ^ 5
1 <= 'data' in any node <= 10^5
Time limit: 1 sec
Can you solve this problem using stack?
Below is the detailed algorithm:
O(n), where ‘n’ is the number of nodes in the tree.
We are visiting every node of the tree once. Hence, the overall time complexity is O(n).
O(n), where ‘n’ is the length of the list.
O(n) extra space is required for the stack. Hence, overall space complexity is O(n).