Loading problem…
You're building a form builder or state management system that needs to persist complex nested state (a tree) to localStorage or send it over an API.
Implement two functions:
serialize(root) → returns a string representation of the binary treedeserialize(data) → reconstructs the original tree from the stringThere is no restriction on the encoding format, but it must satisfy:
deserialize(serialize(root)) reconstructs the same structure and values.null children.Use level-order (BFS) values separated by commas, using the literal string "null" for missing nodes.
For stability, trim trailing `null`s in the serialized output.
Tree: [1,2,3,null,null,4,5]
Serialized: "1,2,3,null,null,4,5"
Example 1:
Example 2:
Example 3:
Example 4: