0
0
DSA C++programming~10 mins

Serialize and Deserialize Binary Tree in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Serialize and Deserialize Binary Tree
Start Serialization
Visit Node
Serialize Left
Add Node Value or Null
End Serialization
Start Deserialization
Read Next Value
Return Null
Deserialize Left
Return Node
End Deserialization
Serialization visits nodes in preorder, recording values or nulls; deserialization reads values to rebuild the tree recursively.
Execution Sample
DSA C++
void serialize(TreeNode* root, string& out) {
  if (!root) { out += "#,"; return; }
  out += to_string(root->val) + ",";
  serialize(root->left, out);
  serialize(root->right, out);
}

TreeNode* deserialize(istringstream& in) {
  string val;
  getline(in, val, ',');
  if (val == "#") return nullptr;
  TreeNode* node = new TreeNode(stoi(val));
  node->left = deserialize(in);
  node->right = deserialize(in);
  return node;
}
This code serializes a binary tree into a string with preorder traversal and '#' for nulls, then deserializes it back.
Execution Table
StepOperationNode VisitedPointer ChangesTree State (Preorder)
1Serialize root1out += '1,'1
2Serialize left child2out += '2,'1 -> 2
3Serialize left.left (null)nullout += '#,'1 -> 2 -> #
4Serialize left.right (null)nullout += '#,'1 -> 2 -> # -> #
5Serialize right child3out += '3,'1 -> 2 -> # -> # -> 3
6Serialize right.left (null)nullout += '#,'1 -> 2 -> # -> # -> 3 -> #
7Serialize right.right (null)nullout += '#,'1 -> 2 -> # -> # -> 3 -> # -> #
8Serialization completeN/Aout = '1,2,#,#,3,#,#,'Full serialized string
9Deserialize root1Create node 11
10Deserialize left child2Create node 21 -> 2
11Deserialize left.leftnullReturn null1 -> 2 -> null
12Deserialize left.rightnullReturn null1 -> 2 -> null -> null
13Deserialize right child3Create node 31 -> 2 -> null -> null -> 3
14Deserialize right.leftnullReturn null1 -> 2 -> null -> null -> 3 -> null
15Deserialize right.rightnullReturn null1 -> 2 -> null -> null -> 3 -> null -> null
16Deserialization completeN/ATree rebuiltTree structure restored
💡 All nodes visited and serialized/deserialized; nulls marked with '#' to preserve structure.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5After Step 8After Step 9After Step 16
out (string)"""1,""1,2,""1,2,#,#,3,""1,2,#,#,3,#,#,""1,2,#,#,3,#,#,""1,2,#,#,3,#,#,"
current node (serialize)root (1)2null3nullN/AN/A
input stream (deserialize)emptyreading '1'reading '2'reading '3'reading '#'reading '#'end
current node (deserialize)nullnode 1 creatednode 2 creatednode 3 creatednullnulltree root
Key Moments - 3 Insights
Why do we add '#' for null nodes during serialization?
Adding '#' marks where a node is missing, so during deserialization we know exactly where to place nulls, preserving tree shape (see execution_table rows 3,4,6,7).
How does deserialization know when to stop reading children?
When it reads '#', it returns null immediately, stopping further recursion for that branch (see execution_table rows 11,12,14,15).
Why is preorder traversal used for serialization?
Preorder visits root before children, so the first value is always the root, making it easy to reconstruct the tree recursively (see execution_table steps 1,9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the serialized string after step 8?
A"1,2,#,#,3,#,#,"
B"1,2,3,#,#,#,#,"
C"1,#,2,#,3,#,#,"
D"#,#,1,2,3,#,#,"
💡 Hint
Check the 'out' variable in variable_tracker after step 8.
At which step does deserialization create the right child node with value 3?
AStep 15
BStep 10
CStep 13
DStep 9
💡 Hint
Look at execution_table rows describing deserialization node creation.
If we remove '#' for null nodes during serialization, what happens during deserialization?
ADeserialization runs faster
BTree structure cannot be correctly rebuilt
CNo change in tree structure
DDeserialization creates extra nodes
💡 Hint
Refer to key_moments about why '#' is needed for null nodes.
Concept Snapshot
Serialize and Deserialize Binary Tree:
- Serialize with preorder traversal
- Use '#' to mark null nodes
- Store values separated by commas
- Deserialize by reading values recursively
- '#' means return null node
- Preserves exact tree structure
Full Transcript
Serialization of a binary tree uses preorder traversal to visit nodes. Each node's value is added to a string, separated by commas. Null children are marked with '#'. This string fully represents the tree structure. Deserialization reads this string token by token. When it reads a value, it creates a node; when it reads '#', it returns null. This recursive process rebuilds the original tree exactly. The key is marking nulls to preserve shape. Preorder ensures root is first, simplifying reconstruction.