Posts Serialize and Deserialize Binary Tree
Post
Cancel

Serialize and Deserialize Binary Tree

PROBLEM DESCRIPTION

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

leetcode

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Codec {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        
        //Example: [1, 2, N, N, 3, 4, N, N, 5, N, N] -> preorder
        //N -> null
        List<String> list = serializeHelper(root, new ArrayList<>());

        //Convert the above list of strings to a single string separated by comma
        return covertListToStringWithDelimiter(list);

    }

    //convert list of string to a single string delimited by comma
    public String covertListToStringWithDelimiter(List<String> list){

        StringBuffer sb = new StringBuffer();
        
        //if the list does not have any string, return empty string
        if(list.size() == 0) return "";

        //add the first string
        sb.append(list.get(0));

        //for subsequent string add a comma and then the next string
        for(int i=1; i<list.size(); i++){
            sb.append("," + list.get(i));
        }

        return sb.toString();
    }

    //pre-order traversal
    public List<String> serializeHelper(TreeNode root, List<String> list) {

        if(root == null){
            list.add("N");
            return list;
        }

        list.add(String.valueOf(root.val));
        serializeHelper(root.left, list);
        serializeHelper(root.right, list);

        return list;

    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {

        //split the string by "," to get the array of strings
        String[] arr = data.split(",");

        //covert to list to make it easy to remove the first element
        List<String> data_list = new LinkedList<String>(Arrays.asList(arr));

        return deserializeHelper(data_list);
    }


    public TreeNode deserializeHelper(List<String> list) {

        //if the first element is N, that means it's null, we can move forward since default value will be null for next and right 
        if(list.get(0).equals("N")){
            list.remove(0);
            return null;
        }

        //if it's not null, create a node using the value as int
        int val = Integer.parseInt(list.get(0));
        TreeNode node = new TreeNode(val);

        //remove it from the list
        list.remove(0);

        //recursively set its left pointer and right pointers
        //since we know that the list "pre-ordered", the immediate next will be on the left of it
        //it will keep getting removed from the list until we get a null (base case)
        //at that point, the node.right will get called
        node.left = deserializeHelper(list);
        node.right = deserializeHelper(list);

        return node;

    }

    

}

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));
This post is licensed under CC BY 4.0 by the author.