0
0
DSA Javascriptprogramming~10 mins

Serialize and Deserialize Binary Tree in DSA Javascript - 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
Repeat for all nodes
End Serialization
Start Deserialization
Read Next Value
If value is null -> return null
Create Node
Deserialize Left
Repeat for all values
End Deserialization
Serialization visits each node and records its value or null for missing nodes. Deserialization reads values and rebuilds the tree recursively.
Execution Sample
DSA Javascript
function serialize(root) {
  if (!root) return 'null,';
  return root.val + ',' + serialize(root.left) + serialize(root.right);
}

function deserialize(data) {
  const nodes = data.split(',');
  function build() {
    const val = nodes.shift();
    if (val === 'null' || val === '') return null;
    const node = { val: Number(val), left: build(), right: build() };
    return node;
  }
  return build();
}
This code converts a binary tree to a string and back using preorder traversal with 'null' placeholders.
Execution Table
StepOperationNodes in Tree (Preorder)Pointer ChangesVisual State
1Serialize root node (1)1root -> 11
2Serialize left child (2)1 -> 2current -> 21 / 2
3Serialize left child of 2 (null)1 -> 2 -> nullcurrent -> null1 / 2 / null
4Serialize right child of 2 (null)1 -> 2 -> null -> nullcurrent -> null1 / 2 / null null
5Serialize right child (3)1 -> 2 -> null -> null -> 3current -> 31 / 2 3 / null null
6Serialize left child of 3 (4)1 -> 2 -> null -> null -> 3 -> 4current -> 41 / 2 3 / 4 null
7Serialize left child of 4 (null)1 -> 2 -> null -> null -> 3 -> 4 -> nullcurrent -> null1 / 2 3 / 4 null / null
8Serialize right child of 4 (null)1 -> 2 -> null -> null -> 3 -> 4 -> null -> nullcurrent -> null1 / 2 3 / 4 null / null null
9Serialize right child of 3 (null)1 -> 2 -> null -> null -> 3 -> 4 -> null -> null -> nullcurrent -> null1 / 2 3 / 4 null null / null null
10Deserialization startsnodes array reset
11Read value '1', create node 11root -> 11
12Read value '2', create left child1 -> 2current -> 21 / 2
13Read value 'null', left child of 2 is null1 -> 2 -> nullcurrent -> null1 / 2 / null
14Read value 'null', right child of 2 is null1 -> 2 -> null -> nullcurrent -> null1 / 2 / null null
15Read value '3', create right child1 -> 2 -> null -> null -> 3current -> 31 / 2 3 / null null
16Read value '4', create left child of 31 -> 2 -> null -> null -> 3 -> 4current -> 41 / 2 3 / 4 null
17Read value 'null', left child of 4 is null1 -> 2 -> null -> null -> 3 -> 4 -> nullcurrent -> null1 / 2 3 / 4 null / null
18Read value 'null', right child of 4 is null1 -> 2 -> null -> null -> 3 -> 4 -> null -> nullcurrent -> null1 / 2 3 / 4 null / null null
19Read value 'null', right child of 3 is null1 -> 2 -> null -> null -> 3 -> 4 -> null -> null -> nullcurrent -> null1 / 2 3 / 4 null null / null null
20Deserialization completeTree rebuiltroot returned1 / 2 3 / 4 null null / null null
💡 All nodes processed; serialization and deserialization complete
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
Serialized String"""1,""1,2,""1,2,null,""1,2,null,null,""1,2,null,null,3,""1,2,null,null,3,4,""1,2,null,null,3,4,null,""1,2,null,null,3,4,null,null,""1,2,null,null,3,4,null,null,null,""1,2,null,null,3,4,null,null,null,""1,2,null,null,3,4,null,null,null,"
Deserialization Nodes Array[]["1","2","null","null","3","4","null","null","null",""]["2","null","null","3","4","null","null","null",""]["null","null","3","4","null","null","null",""]["null","3","4","null","null","null",""]["3","4","null","null","null",""]["4","null","null","null",""]["null","null","null",""]["null","null",""]["null",""][""][]
Current Nodenull12nullnull34nullnullnullnullnull
Key Moments - 3 Insights
Why do we add 'null' strings during serialization?
The 'null' strings represent missing children so the tree structure can be fully reconstructed during deserialization, as shown in steps 3, 4, 7, 8, 9 in the execution_table.
How does deserialization know when to stop creating nodes?
Deserialization stops creating nodes when it reads a 'null' value from the serialized data, returning null for that child, as seen in steps 13, 14, 17, 18, 19.
Why do we use preorder traversal for serialization?
Preorder traversal records the root before its children, allowing deserialization to rebuild the tree top-down correctly, as the execution_table shows visiting root first then left and right.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6. What node is currently being serialized?
ANode with value 3
BNode with value 4
CNull node
DNode with value 2
💡 Hint
Check the 'Operation' and 'Nodes in Tree' columns at step 6 in execution_table
At which step does deserialization create the left child of node 3?
AStep 15
BStep 16
CStep 17
DStep 18
💡 Hint
Look for 'create left child of 3' in the 'Operation' column of execution_table
If we remove all 'null' entries from the serialized string, what will happen during deserialization?
ADeserialization will fail to reconstruct the tree correctly
BDeserialization will succeed without issues
CThe tree will be serialized with extra nodes
DThe serialized string will be shorter but correct
💡 Hint
Refer to key_moments about why 'null' placeholders are needed
Concept Snapshot
Serialize: Traverse tree preorder, record node values and 'null' for missing nodes.
Deserialize: Read values in order, create nodes or nulls recursively.
Use commas to separate values.
Preorder ensures root is first, enabling correct tree rebuild.
'null' placeholders keep tree shape intact.
Full Transcript
Serialization of a binary tree means converting it into a string that records each node's value and uses 'null' for missing children. This is done using preorder traversal: visit root, then left subtree, then right subtree. Deserialization reads this string and rebuilds the tree by creating nodes or nulls in the same order. The execution table shows each step of serialization adding values or 'null' to the string, and deserialization reading these values to reconstruct the tree. Key moments clarify why 'null' is necessary to keep the tree shape and why preorder traversal is used. The visual quiz tests understanding of these steps and the importance of 'null' placeholders.