Serialize and deserialize BT leet code problem

1 min readMar 14, 2024

Leet code problem:

Binary tree serialization differs from binary search tree (BST) serialization. In BST serialization, we don’t need to encode NULL children. However, in BT serialization, it is required. The reason is that while constructing a BST, we check if the value is less than or greater than the node value, and based on that, we create left and right children. But in the case of BT, there may or may not be a left child. Therefore, we need to mark null children to preserve the right child and other children of these children.

I tried to use BFS approach and again C language. Here is the link :

When using the BFS approach, we need to use enqueue and dequeue methods. We will enqueue all the children of one level, then dequeue that level and enqueue the next level’s children.

In the case of DFS approach, we simply use in-order/pre-order/post-order recursive methods.