Zero To DSAZero To DSA
Privacy Policy路FAQ路Report a Bug路Support on Ko鈥慺i
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.

C#C#
public class Codec {
    public string Serialize(TreeNode root) {
        // TODO: implement your solution here
        return "";
    }

    public TreeNode Deserialize(string data) {
        // TODO: implement your solution here
        return null;
    }
}

Constraints

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

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.