Coding Trainer

Serialize and Deserialize Binary Tree

HardBinary Treek-trees-graphsLC #297

Problem

Serialize and Deserialize Binary Tree

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

There is no restriction on how your serialization/deserialization algorithm should work; you just need to ensure a binary tree can be serialized to a string, and that string can be deserialized back to the original tree.

Example 1:

Input: root = [1,2,3,null,null,4,5]
Output after serialize+deserialize: [1,2,3,null,null,4,5]

Example 2:

Input: root = []
Output after serialize+deserialize: []

Constraints:

  • The number of nodes is in the range [0, 10⁴]
  • -1000 ≤ Node.val ≤ 1000