Step 1: Start serialization at root node 1
Serialized string: "1,"
Why: We record the root value first
Step 2: Serialize left subtree of node 1 (node 2)
Serialized string: "1,2,"
Why: We record left child value
Step 3: Serialize left and right children of node 2 (both null)
Serialized string: "1,2,null,null,"
Why: We mark null children to keep tree shape
Step 4: Serialize right subtree of node 1 (node 3)
Serialized string: "1,2,null,null,3,"
Why: We record right child value
Step 5: Serialize left child of node 3 (node 4)
Serialized string: "1,2,null,null,3,4,"
Why: We record left child of node 3
Step 6: Serialize left and right children of node 4 (both null)
Serialized string: "1,2,null,null,3,4,null,null,"
Why: Mark null children to keep structure
Step 7: Serialize right child of node 3 (node 5)
Serialized string: "1,2,null,null,3,4,null,null,5,"
Why: Record right child of node 3
Step 8: Serialize left and right children of node 5 (both null)
Serialized string: "1,2,null,null,3,4,null,null,5,null,null,"
Why: Mark null children to keep tree shape
Step 9: Start deserialization from serialized string
Tokens: [1,2,null,null,3,4,null,null,5,null,null]
Why: We split string to rebuild tree nodes
Step 10: Create root node 1
Tree: 1
Step 11: Create left child node 2
Tree: 1
/
2
Why: Next token is left child
Step 12: Left and right children of 2 are null, so no nodes created
Tree unchanged
Why: Null tokens mean no child
Step 13: Create right child node 3
Tree: 1
/ \
2 3
Why: Next token is right child
Step 14: Create left child node 4 of node 3
Tree: 1
/ \
2 3
/
Why: Next token is left child of 3
Step 15: Left and right children of 4 are null
Tree unchanged
Why: Null tokens mean no child
Step 16: Create right child node 5 of node 3
Tree: 1
/ \
2 3
/ \
4 5
Why: Next token is right child of 3
Step 17: Left and right children of 5 are null
Tree unchanged
Why: Null tokens mean no child
Result: Final tree reconstructed:
1
/ \
2 3
/ \
4 5