cheetcode

60. Serialize and Deserialize Binary TreeHard

Turn a binary tree into a string and back again without losing structure.

serialize should convert a binary tree into a string. deserialize should rebuild a tree with the exact same values and shape.

Missing children must be represented somehow; otherwise different tree shapes could produce the same serialized output.

The round trip deserialize(serialize(root)) should preserve the original tree structure.

Examples

Input: operations = ["Codec","serialize","deserialize","serialize"], values = [null,[1,2,3,null,null,4,5],"1,2,3,null,null,4,5",[1,2,3,null,null,4,5]]
Output: [null,"1,2,3,null,null,4,5",[1,2,3,null,null,4,5],"1,2,3,null,null,4,5"]
Input: operations = ["Codec","serialize","deserialize"], values = [null,[],""]
Output: [null,"",[]]

Constraints

  • The number of nodes in the tree is in the range [0, 10,000].
  • -1,000 <= Node.val <= 1,000