0
0
DSA Typescriptprogramming~30 mins

Serialize and Deserialize Binary Tree in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
Serialize and Deserialize Binary Tree
📖 Scenario: Imagine you are building a system that needs to save and load family trees. Each family tree is a binary tree where each person has up to two children. To save the tree to a file or send it over the network, you need to convert it into a string (serialize). Later, you want to rebuild the exact same tree from that string (deserialize).
🎯 Goal: You will create two functions: one to convert a binary tree into a string, and another to convert that string back into the original binary tree. This will help you save and load family trees easily.
📋 What You'll Learn
Define a binary tree node class called TreeNode with val, left, and right properties.
Create a function serialize that converts a binary tree into a string using preorder traversal and uses 'null' for empty nodes.
Create a function deserialize that converts the string back into the original binary tree.
Test the functions with a sample tree and print the serialized string and the root node's value after deserialization.
💡 Why This Matters
🌍 Real World
Saving and loading complex data structures like family trees, decision trees, or game states in a format that can be stored or sent over a network.
💼 Career
Understanding serialization and deserialization is important for backend development, data storage, network communication, and working with APIs.
Progress0 / 4 steps
1
Create the TreeNode class and sample tree
Define a class called TreeNode with a constructor that takes a number val and optional left and right nodes. Then create a sample tree with root value 1, left child 2, and right child 3 where 3 has children 4 and 5.
DSA Typescript
Hint

Remember to set left and right to null by default in the constructor.

2
Create the serialize function
Write a function called serialize that takes a TreeNode | null called root and returns a string. Use preorder traversal (root, left, right). Represent null nodes as the string 'null'. Separate values with commas.
DSA Typescript
Hint

Use a recursive function that returns 'null' for empty nodes and concatenates values with commas.

3
Create the deserialize function
Write a function called deserialize that takes a string data and returns a TreeNode | null. Split the string by commas into an array. Use a helper function that reads the next value, returns null if it is 'null', or creates a TreeNode and recursively builds left and right children.
DSA Typescript
Hint

Use a recursive helper function that reads values one by one from the array and builds the tree.

4
Test serialization and deserialization
Call serialize with the root tree and store the result in data. Then call deserialize with data and store the result in newRoot. Finally, print data and newRoot.val.
DSA Typescript
Hint

Print the serialized string and the value of the root node of the deserialized tree.