1. Preorder With a Mark for Every Missing Child Optimal
Intuition
Values alone are not enough: another tree can list the same values in the same order. So serialize writes the tree in preorder, a node's value before its left subtree and its right, with N for every missing child and a comma between values. Every N says where a branch ends, so deserialize, reading the values back in the same order, always knows which side the next value belongs to: after a node's two N's it is finished, and the next value goes to the side still open.
Algorithm
1. serialize: write(node) appends N for a missing child, else the node's value, then write(left), then write(right); join the values with commas.
2. deserialize: split the string at the commas and hand the values over one at a time.
3. build() takes the next value: N is a missing child (return null); otherwise make a node with the value, build its left subtree, then its right, and return it.
Time & Space
Time O(n): each method visits every node and every missing child once. Space O(n): the string holds every value and every N, and the calls go as deep as the tree is tall.