297. 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 then it can be stored in A file or memory buffer, or transmitted across a network connection link to being reconstructed later in the same or another Computer environment.

Design a algorithm to serialize and deserialize a binary tree. There is no restriction on what your serialization/deserialization algorithm should work. You just need to ensure, a binary tree can is serialized to a string and this string can is deserialized to the origin AL tree structure.

For example, serialize the following tree

    1   /   2   3     /     4   5

"[1,2,3,null,null,4,5]"as, just the same as how Leetcode OJ serializes a binary tree. You don't necessarily need to follow the this format, so that you are 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.

This kind of serialization method is very good-looking, uses is preorder, the use is very wide, the practice is also quite beautiful

1. Find a large tree in the cc150 A is not another large tree B of the subtree, can also be squashed first, and then if a tree sequence is a substring of another sequence of numbers, then it is a sub-tree

2. Snapchat has a face, "it is from the binary tree to find all the duplicate trees." Return should be a key value Pair,key is any representation of the tree, value is the number of times the tree appears. My general idea is to make a serialization of each node, the tree that is rooted in this node. Then take this serialization as key and throw it into the hashtable. Followup is to ask about the complexity of time and space. "It's also possible to flatten every node and then count the number of occurrences with hashset.

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) { -StringBuilder SB =NewStringBuilder (); the getserialization (root, SB); -         returnsb.tostring (); -     } -      +     Private voidgetserialization (TreeNode root, StringBuilder sb) { -         if(Root = =NULL) { +Sb.append ("#"). Append (","); A             return; at         } -Sb.append (Root.val + ","); - getserialization (Root.left, SB); - getserialization (Root.right, SB); -     } -  in     //decodes your encoded data to tree. -      PublicTreeNode Deserialize (String data) { tostring[] nodes = Data.split (","); +queue<string> queue =NewLinkedlist<>(); - Queue.addall (Arrays.aslist (nodes)); the         returngettree (queue); *     } $     Panax Notoginseng     PrivateTreeNode Gettree (queue<string>queue) { -String cur =Queue.poll (); theTreeNode root =NULL; +         if(Cur.equals ("#")) { A             returnRoot; the}Else { +Root =NewTreeNode (integer.parseint (cur)); -Root.left =gettree (queue); $Root.right =gettree (queue); $         } -         returnRoot; -     } the } - Wuyi //Your Codec object would be instantiated and called as such: the //Codec Codec = new Codec (); - //Codec.deserialize (Codec.serialize (root));

297. 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.