Programming questions - serialization and deserialization

Title Description

Please implement two functions to serialize and deserialize binary tree


Ideas: (1) first, we need to build a binary tree

The idea of building a binary tree: Step 1: encapsulate the elements in the array into TreeNode type nodes and load them into the nodeList list.

Step 2: build a binary tree according to the mathematical relationship between the parent node of the binary tree and its corresponding child paper node.

The specific relationship is: the subscript of the parent node is 0-n/2-1 (but the termination condition is less than n/2-1), because the last parent node may not have a right child; when n/2-1 is an odd number, the parent node has a right child; when n/2-1 is an even number, the parent node does not have a right child Children.

The next step of serialization is to convert the tree into a string (for example, between each node! Separated. Empty nodes are represented by ×).

Last deserialization is to convert the string to the original array sequence.


The code is as follows:


import java.util.LinkedList;
import java.util.List;

 * Serializing and deserializing binary trees
 * SerializeTree / Deserialize
public class SerializeTree {
    //Define TreeNode class
    public static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val){
            this.left = null;
            this.right = null;
            this.val = val;

    public static int[] array = {1,2,3,4,5,6,7,8,9};
    public static List<TreeNode> nodeList = null;//Define a linked list to store binary trees

    //Building a binary tree
    private static void createBinaryTree(int[] array){
        nodeList = new LinkedList<>();
        //Convert the value of an array to a TreeNode node
        for(int i=0;i < array.length; i++){
            nodeList.add(new TreeNode(array[i]));
        //For the first (parentIndex-1) parent nodes, a binary tree relationship is established according to the number relationship with the child
        for(int parentIndex=0; parentIndex < array.length/2-1; parentIndex++){
            //Left child
            nodeList.get(parentIndex).left = nodeList.get(parentIndex*2+1);
            //Right child
            nodeList.get(parentIndex).right = nodeList.get(parentIndex*2+2);

        //For the (parentIndex-1) parent node, construct the left and right subtrees
        int mid = array.length/2 - 1;
        //Left child
        nodeList.get(mid).left = nodeList.get(mid*2+1);
        //Right child, if the length of the array is odd, then there is a right subtree
        if(mid%2 == 1){
            nodeList.get(mid).right = nodeList.get(mid*2+2);

    private static String Serialize(TreeNode root){
        StringBuilder str = new StringBuilder();
        if(root == null){
            return str.toString();

        return str.toString();

    //De serialization
    public static int index = -1;
    private static TreeNode Deserialize(String str){

        if(index >= str.length()) {
            return null;
        String[] nodes = str.split("!");
        TreeNode root = null;
            root = new TreeNode(Integer.valueOf(nodes[index]));
            root.left = Deserialize(str);
            root.right = Deserialize(str);
        return root;

    public static void main(String[] args){
        System.out.println("For array 1,2,3,4,5,6,7,8,9 To serialize and deserialize:");
        SerializeTree tree = new SerializeTree();
        tree.createBinaryTree(array);//Create a binary tree
        TreeNode root = nodeList.get(0);//Get root node

        String res = tree.Serialize(root);

        TreeNode newRoot=tree.Deserialize(res);



Tags: Java less

Posted on Sat, 09 Nov 2019 11:11:48 -0500 by pietbez