Leetcode:serialize and Deserialize Binary Tree

Source: Internet
Author: User

serialization is the process of converting a data structure or object to a sequence of bits so that it can stored In a file or memory buffer, or transmitted across a network connection link to being reconstructed later in the same or anot Her computer environment. Design a 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, a binary tree can be serialized to a string and Thisstring can is deserialized to the original tree structure. For example, serialize the following tree1/   2 3/     4 5 as"[1,2,3,null,null,4,5]", just the same as how Leetcode OJ serializes a binary tree. You DoNot necessarily need to follow Thisformat, so is creative and come up with different approaches yourself. Note:do not useclassmember/global/Staticvariables to store states. Your serialize and deserialize algorithms should be stateless.

Reference lintcode:http://www.cnblogs.com/edwardliu/p/4391418.html

Either serialize or deserialize is a tree level order traversal

Serialize encountered a null node, adding "#" to the result, but not into the queue

Deserialize encountered "#", add null to cur TreeNode left child or right child, but the child does not enter the queue

My methods are in accordance with the Leetcode standard to remove the "#" at the end of serialized, deserialize if you go to the tail of the array but the queue is not empty, then all the nodes in the queue give them add empty left and right child

Again, the comparison of string values is equals (), not paying attention and writing = =, it takes a lot of time to debug

1 /**2 * Definition for a binary tree node.3 * public class TreeNode {4 * int val;5 * TreeNode left;6 * TreeNode right;7 * TreeNode (int x) {val = x;}8  * }9  */Ten  Public classCodec { One  A     //encodes a tree to a single string. -      PublicString serialize (TreeNode root) { -         if(Root = =NULL)return""; theStringBuffer res =NewStringBuffer (); -linkedlist<treenode> queue =NewLinkedlist<treenode>(); - Queue.offer (root); -Res.append (Root.val + ""); +          while(!Queue.isempty ()) { -TreeNode cur =Queue.poll (); +TreeNode left =Cur.left; ATreeNode right =Cur.right; at             if(left = =NULL) { -Res.append ("#" + "" "); -             } -             Else { -Res.append (Left.val + ""); - Queue.offer (left); in             } -             if(right = =NULL) { toRes.append ("#" + "" "); +             } -             Else { theRes.append (Right.val + ""); * Queue.offer (right); $             }Panax Notoginseng         } -         if(Res.charat (Res.length ()-1) = = ") Res.deletecharat (Res.length ()-1); the         intK = Res.length ()-1; +          while(K>=0 && Res.charat (k) = = ' # ') { A Res.deletecharat (k); theRes.deletecharat (k-1); +K-= 2; -         } $         returnres.tostring (); $     } -  -     //decodes your encoded data to tree. the      PublicTreeNode Deserialize (String data) { -         if(data==NULL|| Data.length () ==0)return NULL;Wuyistring[] nodes = Data.split (""); thelinkedlist<treenode> queue =NewLinkedlist<treenode>(); -TreeNode root =NewTreeNode (Integer.parseint (nodes[0])); Wu Queue.offer (root); -         inti = 1; About          while(!queue.isempty () && i<nodes.length) { $TreeNode cur =Queue.poll (); -String left =Nodes[i]; -i++; -             if(Left.equals ("#")) Cur.left =NULL; A             Else { +Cur.left =NewTreeNode (Integer.parseint (left)); the Queue.offer (cur.left); -             } $             if(i = = nodes.length) Break; theString right =Nodes[i]; thei++; the             if(Right.equals ("#")) Cur.right =NULL; the             Else { -Cur.right =NewTreeNode (Integer.parseint (right)); in Queue.offer (cur.right); the             } the         } About          while(!Queue.isempty ()) { theTreeNode cur =Queue.poll (); theCur.left =NULL; theCur.right =NULL; +         } -         returnRoot; the     }Bayi } the  the //Your Codec object would be instantiated and called as such: - //Codec Codec = new Codec (); - //Codec.deserialize (Codec.serialize (root));

Leetcode:serialize and Deserialize Binary Tree

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.