31 / 75
H

Serialize and Deserialize Binary Tree

#31
treedfsbfs+2

Convert tree to string and back using BFS level-order or DFS pre-order traversal.

Example:

Input:root=[1,2,3,null,null,4,5]
Output:Serialized string that deserializes back to same tree

Common Mistakes:

  • Not handling empty tree (null root)
  • Forgetting to serialize null children (loses structure)
  • Index out of bounds in deserialization
  • Not choosing a delimiter that won't conflict with node values

Notes:

BFS approach shown. Can also use DFS pre-order. Time O(n) for both, Space O(n). Format: '1,2,3,null,null,4,5,'

31/75
Serialize and Deserialize Binary Tree