Binary Tree & BST Serialize & Deserialize

Binary

Binary Tree Serialize with Preorder

public class Codec {

    private final static String NULL = "#";
    private final static String SEP = ",";
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        serialize(root, sb);
        return sb.toString();
    }

    private void serialize(TreeNode root, StringBuilder sb) {
        if (root == null) {
            sb.append(NULL).append(SEP);
            return;
        }
        sb.append(root.val).append(SEP);
        serialize(root.left, sb);
        serialize(root.right, sb);
    }

    // Decodes your encoded data to tree.
    // Or can use an external index
    public TreeNode deserialize(String data) {
        LinkedList<String> nodeStrList = new LinkedList<>(Arrays.asList(data.split(SEP)));
        return deserialize(nodeStrList);
    }

    private TreeNode deserialize(LinkedList<String> nodeStrList) {
        if (nodeStrList.isEmpty()) {
            return null;
        }
        String nodeStr = nodeStrList.removeFirst();
        if (nodeStr.equals(NULL)) {
            return null;
        }
        TreeNode node = new TreeNode(Integer.parseInt(nodeStr));
        node.left = deserialize(nodeStrList);
        node.right = deserialize(nodeStrList);
        return node;
    }
}

BST serializes & deserialize, the difference is that, for BST, the preorder traverse can determine the unique tree, so there is not need to add the "#" to replace the empty nodes

public class Codec {

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

    private void serialize(TreeNode root, StringBuilder sb) {
        if (root == null) {//Differences
            return;
        }
        sb.append(root.val).append(SEP);
        serialize(root.left, sb);
        serialize(root.right, sb);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if (data == null || data.length() == 0) {
            return null;
        }
        String[] dataArr = data.split(SEP);
        return deserialize(dataArr, 0, dataArr.length - 1);
    }

    private TreeNode deserialize(String[] dataArr, int low, int high) {
        if (low > high) { //Differences 
            return null;
        }
        String nodeStr = dataArr[low];
        int nodeVal = Integer.parseInt(nodeStr);
        TreeNode root = new TreeNode(nodeVal);

        //The first element index of the right children in the array
        int index =  low + 1; //Need to find the split of the left tree and right tree.
        while (index <= high && Integer.parseInt(dataArr[index]) < nodeVal) {
            index++;
        }
        root.left = deserialize(dataArr, low + 1, index - 1);
        root.right = deserialize(dataArr, index, high);
        return root;
    }
}

Last updated