AdSense

Sunday, October 9, 2016

Serialize and Deserialize Binary Tree

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.
For example, you may serialize the following tree
    1
   / \
  2   3
     / \
    4   5
as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

Use any-order traversal to serialize the tree and then deserialize it. Note for serialization we cannot save any state so no global variable can be used. I convert the whole string to an iterator for deserialization.


public class Codec {
    // Encodes a tree to a single string.
     public String serialize(TreeNode root) {
        if (root == null)
            return "#,";
        return String.valueOf(root.val) + "," + serialize(root.left) + serialize(root.right);
    }
    

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        return getNode(Arrays.asList(data.substring(0, data.length() - 1).split(",")).iterator());
    }
    private TreeNode getNode(Iterator tree) {
        if (!tree.hasNext()) {
            return null;
        }
        String c = tree.next();
        if ("#".equals(c)) {
            return null;
        }
        TreeNode root = new TreeNode(Integer.parseInt(c));
        root.left = getNode(tree);
        root.right = getNode(tree);
        return root;
    }
}


1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete