[leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树

Posted 程序媛詹妮弗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode]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.

Example: 

You may serialize the following tree:

    1
   /   2   3
     /     4   5

as "[1,2,3,null,null,4,5]"

Clarification: The above format is the same as how LeetCode 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.

 

思路:

 

代码:

 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         buildString(root, sb);
16         return sb.toString();
17     }
18     
19     private void buildString(TreeNode root, StringBuilder sb){
20         if(root == null){
21             // splitter: "," 
22             sb.append("#").append(",");
23         }else{
24             // I use pre-order traversal: root, left, right 
25             sb.append(root.val).append(",");
26             buildString(root.left, sb);
27             buildString(root.right, sb);
28         }
29     } 
30 
31     // Decodes your encoded data to tree.
32     public TreeNode deserialize(String data) {
33         if(data == null) return null;
34         String[]stringArr = data.split(",");
35         Queue<String> queue = new LinkedList<>();
36         // put each item of stringArr into queue
37         Collections.addAll(queue, stringArr);
38         return buildTree(queue);
39     }
40     
41     private TreeNode buildTree(Queue<String> queue){
42         if(queue.isEmpty()) return null;
43         String curStr = queue.poll();
44         if(curStr.equals("#")) return null;
45         // to match serialize traversal order, I use pre-order traversal: root, left, right
46         TreeNode root = new TreeNode(Integer.parseInt(curStr));
47         root.left = buildTree(queue);
48         root.right = buildTree(queue);
49         return root;
50     }    
51 }
52 
53 // Your Codec object will be instantiated and called as such:
54 // Codec codec = new Codec();
55 // codec.deserialize(codec.serialize(root));

 

以上是关于[leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树的主要内容,如果未能解决你的问题,请参考以下文章

[Lintcode]7. Serialize and Deserialize Binary Tree/[Leetcode]297. Serialize and Deserialize Binary T

LeetCode 297: Serialize and Deserialize Binary Tree

LeetCode297. Serialize and Deserialize Binary Tree

[leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree