Lintcode Medium title: Binary tree serialization serialization and deserialization of two forks

Source: Internet
Author: User

Topic

two serialization and deserialization of a fork tree

Design an algorithm and write code to serialize and deserialize the binary tree. Writing a tree to a file is called "serialization," and rebuilding the same two-fork tree after reading the file is called "deserialization."

How to deserialize or serialize a binary tree is unlimited, you only need to make sure that you can serialize the two-fork tree to a string, and you can deserialize the string into the original tree structure.

Sample Example

Give a sample of the test data, a binary tree {3,9,20,#,#,15,7} , representing the following tree structure:

  3 / 9  20  /   15   7

Our data is obtained by the BFS traversal. When you test the results wrong answer, you can debug your code as input.

You can use other methods for serialization and deserialization.

Solving

Refer to the nine chapters procedure

look at the notes and understand, but I'm saying I can't think of them. Java

/*** Definition of TreeNode: * public class TreeNode {* public int val; * Public TreeNode left, right; * PU Blic TreeNode (int val) {* This.val = val; * This.left = This.right = null; *} *}*/classSolution {/*** This method is invoked first, should design your own algorithm * to serialize a binary tree which     Denote by a root node to a string which * can is easily deserialized by your own "deserialize" method later. */     PublicString serialize (TreeNode root) {//Write your code here        if(Root = =NULL)            return"{}"; ArrayList<TreeNode> queue =NewArraylist<treenode>();        Queue.add (root); //store nodes of the two-fork tree in a queue from top to bottom, left to right         for(intI=0;i<queue.size (); i++) {TreeNode Q=Queue.get (i); if(q==NULL)                Continue;            Queue.add (Q.left);        Queue.add (Q.right); }        //remove the left and right child of the leaf node, the child is empty value         while(Queue.get (Queue.size ()-1) = =NULL) {Queue.remove (queue.size ()+ W); }        //traverse the queue to convert to a stringStringBuilder SB =NewStringBuilder (); Sb.append ("{"); Sb.append (Queue.get (0). val);  for(intI=1;i<queue.size (); i++) {TreeNode Q=Queue.get (i); if(q!=NULL) {sb.append (",");            Sb.append (Q.val); }Else{sb.append (",#"); }} sb.append ("}"); returnsb.tostring (); }        /*** This method would be invoked second, the argument data are what exactly * you serialized at method "Serialize ", that means the data was not given by * system, it's given by your own Serialize method. The. The format of data is * designed by yourself, and deserialize it as you serialize it in * "Serialize" met     Hod. */     PublicTreeNode Deserialize (String data) {//Write your code here        if(data = = "{}")            return NULL; //split with commasstring[] Vals = data.substring (1,data.length ()-1). Split (","); ArrayList<TreeNode> queue =NewArraylist<treenode>(); //root nodeTreeNode root =NewTreeNode (Integer.parseint (vals[0]));        Queue.add (root); intindex = 0; BooleanIsleftchild =true;  for(inti = 1; i < vals.length; i++) {            if(!vals[i].equals ("#") {TreeNode node=NewTreeNode (Integer.parseint (vals[i)); if(isleftchild) {queue.get (index). Left=node; } Else{queue.get (index). Right=node;            } queue.add (node); }            if(!isleftchild) {Index++; } isleftchild= !Isleftchild; }        returnRoot; }}

Lintcode Medium title: Binary tree serialization serialization and deserialization of two forks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.