You are given a Binary Search Tree with its root node. You are supposed to serialize a binary search tree into a string and deserialize the string into a binary search tree.
Note :
Serialization is the process of converting an object into a stream of bytes.
Deserialization is the opposite process of creating an object from a stream of bytes.
You can apply your own serialization and deserialization algorithms.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains elements in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
For example, the input for the tree depicted in the below image will be:

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).
Note :
The above format was just to provide clarity on how the input is formed for a given tree.
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
Output Format :
Print the level order traversal of the Binary Search Tree after Deserialization separated by spaces. It should match with the level order traversal of the initially given tree.
Print the answer of each test case in a new line.
Note :
None of the pointers in the new tree should point to nodes in the original tree, otherwise, the expected output can be different.
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N < 10^4
0 <= data < 10^4 and data!=-1
Time Limit: 1 sec
2
24 15 33 12 21 30 36 9 -1 18 -1 27 -1 -1 -1 -1 -1 -1 -1 -1 -1
15 9 21 7 13 19 23 5 -1 11 -1 17 -1 -1 -1 -1 -1 -1 -1 -1 -1
24 15 33 12 21 30 36 9 18 27
15 9 21 7 13 19 23 5 11 17
In the first test case, The values of the nodes are 24, 15, 33, 12, 21, 30, 36, 9, 18, and 27. After performing Serialization and Deserialization, the tree should remain the same. So, the level order traversal of the tree is 24, 15, 33, 12, 21, 30, 36, 9, 18, and 27.
In the second test case, The values of the nodes are 15, 9, 21, 7, 13, 19, 23, 5, 11, and 17. After performing Serialization and Deserialization, the tree should remain the same. So, the level order traversal of the tree is 15, 9, 21, 7, 13, 19, 23, 5, 11, and 17.
2
1 2 3 -1 -1 -1 -1
25 16 34 13 22 31 37 -1 -1 -1 -1 -1 -1 -1 -1
1 2 3
25 16 34 13 22 31 37
Can we separate the node values with space for serialization and separate those for deserialization?
The idea is to perform a recursive Preorder traversal and store the node values in a string separated by spaces. Here we are utilizing the property of the binary search tree, if we do a pre-order traversal of a binary search tree, we’ll get sorted values. For deserialization, first, create the left sub-tree, then create the right sub-tree and then return the root node.
The steps are as follows:
Serialization:
Deserialization:
O(N ), where N is the total number of nodes in the given binary search tree.
As we are visiting every node exactly once. Hence, the time complexity is O(N).
O(N), where N is the total number of nodes in the given binary search tree.
As we are having a function calling stack that stores at most all the nodes of the binary search tree and a string that stores the serialized form of the binary search tree. Hence, the overall space complexity is O(N).