Design a format to convert a binary tree to a string and rebuild the same tree from that string.
Given the root of a binary tree, design two functions:
serialize(root): convert the tree into a string representation.deserialize(data): rebuild the exact same binary tree from that string.Your serialization must preserve the tree structure, including missing children. Any valid encoding is acceptable as long as deserialize(serialize(root)) produces a tree equivalent to the original.
serialize.data for deserialize.serialize(root) returns a string.deserialize(data) returns the root of the reconstructed binary tree.serialize(root): a binary tree root node.deserialize(data): a string produced by serialize.serialize(root) returns a string encoding of the tree.deserialize(data) returns the root node of the reconstructed tree.Example 1
Input
root = [1,2,3,null,null,4,5]
Output
"1,2,#,#,3,4,#,#,5,#,#"
Explanation
One valid preorder serialization uses # for nulls. Deserializing this string reconstructs the same binary tree.
Example 2
Input
root = []
Output
"#"
Explanation
An empty tree can be represented by a single null marker.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.