LeetCode 297: Serialize and Deserialize Binary Tree Solution

Master LeetCode problem 297 (Serialize and Deserialize Binary Tree), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

297. Serialize and Deserialize Binary Tree

Problem Explanation

Explanation

To serialize a binary tree, we can perform a preorder traversal (root-left-right) and store the node values in a string. For deserialization, we can recreate the original tree structure by parsing the serialized string. We use a delimiter to separate node values in the serialized string.

Serialization:

  1. Serialize the root node value.
  2. Recursively serialize the left subtree.
  3. Recursively serialize the right subtree.

Deserialization:

  1. Split the serialized string by the delimiter to get node values.
  2. Reconstruct the tree using the node values in preorder traversal order.

Time Complexity: O(N) where N is the number of nodes in the tree. Space Complexity: O(N) for both serialization and deserialization.

Solution Code

public class Codec {

    private static final String DELIMITER = ",";

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        serializeHelper(root, sb);
        return sb.toString();
    }

    private void serializeHelper(TreeNode node, StringBuilder sb) {
        if (node == null) {
            sb.append("null").append(DELIMITER);
            return;
        }

        sb.append(node.val).append(DELIMITER);
        serializeHelper(node.left, sb);
        serializeHelper(node.right, sb);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        Queue<String> nodes = new LinkedList<>(Arrays.asList(data.split(DELIMITER)));
        return deserializeHelper(nodes);
    }

    private TreeNode deserializeHelper(Queue<String> nodes) {
        String val = nodes.poll();
        if (val.equals("null")) {
            return null;
        }

        TreeNode node = new TreeNode(Integer.parseInt(val));
        node.left = deserializeHelper(nodes);
        node.right = deserializeHelper(nodes);
        return node;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 297 (Serialize and Deserialize Binary Tree)?

This page provides optimized solutions for LeetCode problem 297 (Serialize and Deserialize Binary Tree) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 297 (Serialize and Deserialize Binary Tree)?

The time complexity for LeetCode 297 (Serialize and Deserialize Binary Tree) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 297 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 297 in Java, C++, or Python.

Back to LeetCode Solutions