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 subtree of 1 (node 2)
Serialized string: "1,2,"
Why: We record left child to keep tree structure
Step 3: Serialize left child of 2 which is null
Serialized string: "1,2,null,"
Why: Null marks where branches end
Step 4: Serialize right child of 2 which is null
Serialized string: "1,2,null,null,"
Why: Both children null means leaf node
Step 5: Serialize right subtree of 1 (node 3)
Serialized string: "1,2,null,null,3,"
Why: Continue with right child to keep full tree
Step 6: Serialize left child of 3 (node 4)
Serialized string: "1,2,null,null,3,4,"
Why: Record left child of 3
Step 7: Serialize left and right children of 4 which are null
Serialized string: "1,2,null,null,3,4,null,null,"
Why: Leaf node 4 ends here
Step 8: Serialize right child of 3 (node 5) and its null children
Serialized string: "1,2,null,null,3,4,null,null,5,null,null,"
Why: Finish right subtree serialization
Step 9: Start deserialization from string
Reading "1" creates root node 1
Step 10: Deserialize left subtree from next tokens "2,null,null"
Node 2 created with null children
Why: Rebuild left subtree exactly
Step 11: Deserialize right subtree from remaining tokens "3,4,null,null,5,null,null"
Node 3 with children 4 and 5 rebuilt
Why: Rebuild right subtree exactly
Result: Tree rebuilt exactly as:
1
/ \
2 3
/ \
4 5