Startup VO Interview Coding Question: Serialize and Deserialize Binary Tree

14 Views
No Comments

Given a binary tree, create two functions: one that serializes the binary tree into a string and one that deserializes a serialized string back into a binary tree.

Example:

        1
       / \
      2   3
         / \
        4   5

Serialized string: 1,2,3,null,null,4,5,null,null,null,null

This problem asks you to design binary tree serialization and deserialization routines. The key idea is to preserve tree structure by recording null placeholders in a consistent traversal order, such as preorder DFS or level-order BFS. Serialization writes node values and missing children into a string, while deserialization reads the same sequence and reconstructs the tree recursively or with a queue. The main challenge is keeping the encoding and decoding logic perfectly aligned so the original tree can be rebuilt exactly.

END
 0