Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Serialize and Deserialize Binary Tree
HARD
15 mins
28 upvotes
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Serialize and Deserialize Binary Tree

Contributed by
Shivam Mehla
Hard
yellow-spark
0/120
Avg time to solve 15 mins
Success Rate 85 %
Share
28 upvotes

Problem Statement

You have been given a binary tree of integers. You are supposed to serialize and deserialize (refer to notes) the given binary tree. You can choose any algorithm to serialize/deserialize the given binary tree. You only have to ensure that the serialized string can be deserialized to the original binary tree.

Note :
Serialization is the process of translating a data structure or object state into a format that can be stored or transmitted (for example, across a computer network) and reconstructed later. The opposite operation that is, extracting a data structure from stored information, is deserialization.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2
1 -1 3 -1 -1
3 4 -1 -1 -1
Sample Output 1 :
1 -1 3 -1 -1 
3 4 -1 -1 -1
Explanation For Sample Input 1 :
For the first test case, the level order traversal of the given tree will be “1 -1 3 -1 -1” where -1 denotes the null nodes.

For the second test case,  the level order traversal of the given tree will be “3 4 -1 -1 -1” where -1 denotes the null nodes.
Sample Input 2 :
2
1 2 3 -1 4 5 -1 -1 -1 -1 -1
2 1 3 4 -1 -1 5 -1 -1 -1 -1 
Sample Output 2 :
1 2 3 -1 4 5 -1 -1 -1 -1 -1
2 1 3 4 -1 -1 5 -1 -1 -1 -1
Reset Code
Full screen
Auto
copy-code
Console