首页 > 代码库 > 297. Serialize and Deserialize Binary Tree

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


 

 

这种serialization的方法很好看,用的是preOrder, 用途很广,做法也是挺漂亮的

1. cc150里面的找一个超大的树A是不是另一个超大的树B的子树,也可以先压扁,然后如果一个树的序列是另外一个数序列的子串,那么就是子树

2. snapchat有个面经,“是从binary tree里找出所有duplicate的树。return应该是一个key value pair,key是任何形式表示的树,value是这个树出现的次数。我的大致思路就是对每个node,做出以这个node为根的树的serialization。然后以这个serialization为key扔到hashtable里就可以。followup就是问时间和空间复杂度。”也是可以把每一个node压扁了,然后用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  */
10 public class Codec {
11     
12     // Encodes a tree to a single string.
13     public String serialize(TreeNode root) {
14         StringBuilder sb = new StringBuilder();
15         getSerialization(root, sb);
16         return sb.toString();
17     }
18     
19     private void getSerialization(TreeNode root, StringBuilder sb) {
20         if(root == null) {
21             sb.append("#").append(",");
22             return;
23         }
24         sb.append(root.val + ",");
25         getSerialization(root.left, sb);
26         getSerialization(root.right, sb);
27     }
28 
29     // Decodes your encoded data to tree.
30     public TreeNode deserialize(String data) {
31         String[] nodes = data.split(",");
32         Queue<String> queue = new LinkedList<>();
33         queue.addAll(Arrays.asList(nodes));
34         return getTree(queue);
35     }
36     
37     private TreeNode getTree(Queue<String> queue) {
38         String cur = queue.poll();
39         TreeNode root = null;
40         if(cur.equals("#")) {
41             return root;
42         } else {
43             root = new TreeNode(Integer.parseInt(cur));
44             root.left = getTree(queue);
45             root.right = getTree(queue);
46         }
47         return root;
48     }
49 }
50 
51 // Your Codec object will be instantiated and called as such:
52 // Codec codec = new Codec();
53 // codec.deserialize(codec.serialize(root));

 

297. Serialize and Deserialize Binary Tree