Zero To DSAZero To DSA
Privacy Policy
Lowest Common Ancestor of BST

Serialize and Deserialize Binary Tree

hard
Time: O(n)
Space: O(n)

Design an algorithm to serialize (convert to string) and deserialize (convert back) a binary tree. Use preorder traversal with null markers.

Constraints

  • The number of nodes is in the range [0, 10⁴].

Examples

Input: root = [1,2,3,null,null,4,5]
Output: [1,2,3,null,null,4,5]
Serialize then deserialize produces the same tree.