Serialize and Deserialize Binary Tree

Problem Design an algorithm to serialize a binary tree into a string and deserialize that string back into the identical tree. There is no restriction on the format — it only needs to round-trip.

Input / Output

  • Input: serialize(root) -> string; deserialize(string) -> root.
  • Output: a tree structurally identical to the original.

Constraints

  • Up to 10^4 nodes; values may be negative and multi-digit (delimiters matter).

Example

  • Tree [1,2,3,null,null,4,5] -> "1,2,#,#,3,4,#,#,5,#,#" (preorder with null markers) -> same tree.
asked …
LeaderboardSalaryAccount