Step 1: Start serialization at root node 1
Serialize string: "1"
Why: We record the root value first to start the string
Step 2: Serialize left child 2
Serialize string: "1,2"
Why: We add left subtree values next to keep order
Step 3: Serialize left and right children of 2 which are null
Serialize string: "1,2,null,null"
Why: We add nulls to mark no further children for node 2
Step 4: Serialize right child 3
Serialize string: "1,2,null,null,3"
Why: After left subtree, we serialize right subtree root
Step 5: Serialize left child 4 of node 3
Serialize string: "1,2,null,null,3,4"
Why: Continue preorder traversal to left child of 3
Step 6: Serialize null children of 4
Serialize string: "1,2,null,null,3,4,null,null"
Why: Mark leaf node 4's children as null
Step 7: Serialize right child 5 of node 3
Serialize string: "1,2,null,null,3,4,null,null,5"
Why: Move to right child of 3
Step 8: Serialize null children of 5
Serialize string: "1,2,null,null,3,4,null,null,5,null,null"
Why: Mark leaf node 5's children as null
Step 9: Start deserialization from string
Tokens: [1,2,null,null,3,4,null,null,5,null,null]
Why: We split string to rebuild tree nodes in order
Step 10: Create node 1, then recursively create left subtree from next tokens
Tree root: 1 with left subtree starting at 2
Why: Rebuild tree top-down matching serialization order
Step 11: Create node 2, then assign null children
Left child of 1 is 2 with no children
Why: Leaf node 2 reconstructed with null children
Step 12: Create node 3 as right child of 1, then build its children
Right child of 1 is 3 with children 4 and 5
Why: Rebuild right subtree fully
Step 13: Create node 4 with null children
Left child of 3 is 4 with no children
Why: Leaf node 4 reconstructed
Step 14: Create node 5 with null children
Right child of 3 is 5 with no children
Why: Leaf node 5 reconstructed
Result: Tree rebuilt exactly as:
1
/ \
2 3
/ \
4 5