Problem title
Difficulty
Avg time to solve

K Centers
Moderate
25 mins
Lowest Common Ancestor of a Binary Tree III
Moderate
25 mins
Nodes In Complete Binary Tree
Moderate
15 mins
Verify Preorder Sequence in Binary Search Tree
Moderate
30 mins
Valid Pairing of Numbers
Hard
50 mins
All Binary Trees
Easy
15 mins
Serialize and Deserialize an N-ary tree
Hard
15 mins
Serialize And Deserialize BST
Easy
15 mins
Hard
30 mins
NINJA'S JUMP
Hard
15 mins
6

# Serialize And Deserialize BST

Difficulty: EASY
Contributed By
Avg. time to solve
15 min
Success Rate
85%

Problem Statement

#### 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.
``````
##### Input Format :
``````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.
``````
##### Constraints :
``````1 <= T <= 50
1 <= N < 10^4
0 <= data < 10^4 and data!=-1

Time Limit: 1 sec
``````
##### Sample Input 1 :
``````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
``````
##### Sample Output 1 :
``````24 15 33 12 21 30 36 9 18 27
15 9 21 7 13 19 23 5 11 17
``````
##### Explanation Of Sample Input 1 :
``````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.
``````
##### Sample Input 2 :
``````2
1 2 3 -1 -1 -1 -1
25 16 34 13 22 31 37  -1 -1 -1 -1 -1 -1 -1 -1
``````
##### Sample Output 2 :
``````1 2 3
25 16 34 13 22 31 37
``````
Console