Design an algorithm to serialize and deserialize a binary tree.
Serialization converts a tree into a string format that can be:
You can choose any serialization format, as long as your methods can:
Constraints:
Examples:
// Example 1:
// 1
// / \
// 2 3
// / \
// 4 5
const root1 = createTree([1, 2, 3, null, null, 4, 5]);
const codec = new Codec();
const serialized = codec.serialize(root1);
const deserialized = codec.deserialize(serialized);
console.log(treeToArray(deserialized));
// Output: [1, 2, 3, null, null, 4, 5]
// Example 2:
const root2 = createTree([]);
const serialized2 = codec.serialize(root2);
const deserialized2 = codec.deserialize(serialized2);
console.log(treeToArray(deserialized2));
// Output: []