Step 1: Start serialization at root node 1
Serialized string: "1,"
Why: We record the root value first to know where the tree starts
Step 2: Serialize left child 2
Serialized string: "1,2,"
Why: We record left subtree values next to keep order
Step 3: Serialize left and right children of 2 which are null
Serialized string: "1,2,null,null,"
Why: We mark nulls to know where branches end
Step 4: Serialize right child 3
Serialized string: "1,2,null,null,3,"
Why: After left subtree, we serialize right subtree root
Step 5: Serialize left child 4 of node 3
Serialized string: "1,2,null,null,3,4,"
Why: Continue preorder traversal to left child
Step 6: Serialize left and right children of 4 which are null
Serialized string: "1,2,null,null,3,4,null,null,"
Why: Mark null children to keep structure
Step 7: Serialize right child 5 of node 3
Serialized string: "1,2,null,null,3,4,null,null,5,"
Why: Serialize right child after left subtree
Step 8: Serialize left and right children of 5 which are null
Serialized string: "1,2,null,null,3,4,null,null,5,null,null,"
Why: Mark null children to complete serialization
Step 9: Start deserialization from serialized string
Rebuild node 1
Why: We read root value first to start rebuilding
Step 10: Deserialize left child from next value 2
Rebuild node 2 as left child of 1
Why: Left subtree comes next in preorder
Step 11: Deserialize left and right children of 2 as null
Left and right of 2 are null
Why: Null markers tell us no children here
Step 12: Deserialize right child 3
Rebuild node 3 as right child of 1
Why: Right subtree comes after left subtree
Step 13: Deserialize left child 4 of 3
Rebuild node 4 as left child of 3
Why: Continue preorder deserialization
Step 14: Deserialize left and right children of 4 as null
Left and right of 4 are null
Why: Null markers show no children
Step 15: Deserialize right child 5 of 3
Rebuild node 5 as right child of 3
Why: Finish right subtree deserialization
Step 16: Deserialize left and right children of 5 as null
Left and right of 5 are null
Why: Null markers complete tree
Result: Tree rebuilt exactly as original:
1
/ \
2 3
/ \
4 5