

You are given an N-ary tree where every node has at most ‘N’ child nodes. You need to first serialize it and then deserialize the serialized tree.
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
In simple words, serialization is a process to store a tree in a file so that it can be later restored to its original form. Deserialization is reading the tree back from the file.
Note:Note that the structure of the tree must be maintained while serialization and deserialization. Also, you can use any method to serialize or deserialize the tree. You just need to see that the tree can be serialized into a string and further deserialized into the original tree.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first line of each test case contains elements of the N-ary tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is changed, we take -1. To get the next node
The first not-null node(of the previous level) is treated as the parent of the first nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next 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 below-depicted tree, the input will be given as:
1 -1 2 3 4 -1 5 6 -1 -1 -1 -1 -1

For each test case, you need to print the deserialized tree after its serialization.
Print the output of each test case in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^4
Time limit: 1 sec
1
1 2 3 4 -1 5 6 -1 -1 -1 -1 -1
1 2 3 4 -1 5 6 -1 -1 -1 -1 -1
After deserialization, you need to return the same tree, so print 1 2 3 4 -1 5 6 -1 -1 -1 -1 -1
2
1 2 3 4 5 -1 6 7 8 -1 9 -1 -1 -1 -1 -1 -1 -1
1 2 -1 -1
1 2 3 4 5 -1 6 7 8 -1 9 -1 -1 -1 -1 -1 -1 -1
1 2 -1 -1
In the first test case
After deserialization, you need to return the same tree, so print 1 2 3 4 5 -1 6 7 8 -1 9 -1 -1 -1 -1 -1 -1 -1
In the second test case
After deserialization, you need to return the same tree, so print 1 2 -1 -1
Can you think of breadth-first search traversal of a tree?
Serialization - In serialization, we have to convert the tree into serial order. So we will put the tree node’s value in a vector in such a way that after every node’s child values we will append a ‘-1’.
We will maintain a queue that will store tree nodes. For every node, we will push its child node’s value in the vector and push its child nodes in the queue so that they can be explored further. After pushing all the child node’s value, we will push ‘-1’ to the vector. We will continuously explore all the nodes until the queue is not empty.
Algorithm
1. Serialization
2. Deserialization
For deserialization, we have to convert the values in the vector ‘FILE’ to an N-ary tree. We will take a queue as we have maintained in serialization and initially push a node with data equal to FILE[ 0 ]. As we have appended ‘-1’ after child nodes of every node. So all the nodes before ‘-1’ will become child nodes of the node at the front of the queue.
So we will create nodes until ‘-1’ is encountered. All these nodes will be the child of the node at front of the queue and we will push all these nodes to ‘Q’ so that we can get the child nodes of these nodes also.
Algorithm
O(N), where ‘N’ is the number of nodes of a tree.
Every node of the tree is visited once. Therefore time complexity is of order O(N).
O(N), where ‘N’ is the number of nodes of a tree.
We have used a queue that is storing the child nodes of a node. As the maximum number of children in the tree can be ‘N-1’. So space complexity is O(N).