Skip to Content

Serialize and Deserialize Binary Tree

Home | Coding Interviews | Arrays and Strings | Serialize and Deserialize Binary Tree

Design an algorithm to serialize and deserialize a binary tree into a string.

public class Codec {

// Encodes a tree to a single string.

public String serialize(TreeNode root) {
    if(root == null){
        return "";
    }
    
    return dfs(root);
}

StringBuilder sb = new StringBuilder();
public String dfs(TreeNode node){
    if(node == null){
        sb.append("null, ");
        return "";
    }
    
    sb.append(node.val + ", ");
    
    dfs(node.left);
    dfs(node.right);
    
    return sb.toString();
}



// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
    if(data.equals("")){
        return null;
    }
    String arr[] = data.split(", ");
    return construct(arr);
}

int idx = 0;
public TreeNode construct(String arr[]){
    if(idx >= arr.length || arr[idx].equals("null")){
        idx++;
        return null;
    }
    
    TreeNode node = new TreeNode(Integer.parseInt(arr[idx++]));
    node.left = construct(arr);
    node.right = construct(arr);
    
    return node;
}

Posted by Jamie Meyer 18 days ago