DIY: Serialize and Deserialize Binary Tree

Solve the interview question "Serialize and Deserialize Binary Tree" in this lesson.

Problem statement

For this challenge, you are given a binary tree, and you have to implement two functions:

  • serializer(), This will serialize a binary tree into string.
  • deserializer(), This will deserialize the string into a binary tree.

There are no restrictions on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string, and this string can be deserialized to the original tree structure.

Input

The input for the serializer() function will be the root node of the binary tree containing integer values. The input for the deserializer() function will be the string created as the serializer() function’s output. Let’s say the following tree has to be serialized:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.