2dbi
Home/Adobe/Serialize and Deserialize a Binary Tree
AAdobe·DSASDE-2Onsite – Coding 1

Serialize and Deserialize a Binary Tree

Problem

Design an algorithm to serialize a binary tree to a string and deserialize it back to the original tree structure.

Example

Tree:    1
        / \
       2   3
          / \
         4   5

Serialized: "1,2,null,null,3,4,null,null,5,null,null"
Deserialized: same tree

Constraints

  • The number of nodes ≤ 10^4
  • -1000 ≤ Node.val ≤ 1000

Discussion

  • BFS-based serialization produces a more compact format for balanced trees
  • DFS pre-order is simpler to implement
  • Adobe specifically asks: how do you ensure your deserializer is robust to corrupted input?
added 6 days ago
LeadersAccount