297.Serialize and Deserialize Binary Tree

Posted panini

tags:

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

链接: http://leetcode.com/problems/serialize-and-deserialize-binary-tree/

5/14/2017

算法班,deserialize部分抄的

层序遍历的方法

注意

1. 第36行需要减去最后一个","

2. 第44行将string里面的各个char转为数字和#

3. deserialize部分,TreeNode部分来自于queue,新建的子树node值来自于values

 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     private final String nullNode = "#";
12     private final String delimiter = ",";
13     
14     // Encodes a tree to a single string.
15     public String serialize(TreeNode root) {
16         StringBuilder sb = new StringBuilder();
17         if (root == null) return "#";
18 
19         Queue<TreeNode> queue = new LinkedList<TreeNode>();
20         sb.append("[");
21         queue.offer(root);
22         while (!queue.isEmpty()) {
23             int size = queue.size();
24             for (int i = 0; i < size; i++) {
25                 TreeNode node = queue.poll();
26                 if (node == null) {
27                     sb.append(nullNode);
28                 } else {
29                     sb.append(node.val);
30                     queue.offer(node.left);
31                     queue.offer(node.right);
32                 }
33                 sb.append(delimiter);
34             }
35         }
36         sb.deleteCharAt(sb.length() - 1);
37         sb.append("]");
38         return sb.toString();
39     }
40 
41     // Decodes your encoded data to tree.
42     public TreeNode deserialize(String data) {
43         if (data.equals("#")) return null;
44         String[] values = data.substring(1, data.length() - 1).split(delimiter);
45         Queue<TreeNode> queue = new LinkedList<TreeNode>();
46         TreeNode root = new TreeNode(Integer.parseInt(values[0]));
47         
48         queue.offer(root);
49         for (int i = 1; i < values.length; i++) {
50             // node comes from queue, left/right comes from values
51             TreeNode parent = queue.poll();
52             if (!values[i].equals(nullNode)) {
53                 TreeNode left = new TreeNode(Integer.parseInt(values[i]));
54                 parent.left = left;
55                 queue.add(left);
56             }
57             i++;
58             if (!values[i].equals(nullNode)) {
59                 TreeNode right = new TreeNode(Integer.parseInt(values[i]));
60                 parent.right = right;
61                 queue.add(right);
62             }
63         }
64         return root;
65     }
66 }
67 
68 // Your Codec object will be instantiated and called as such:
69 // Codec codec = new Codec();
70 // codec.deserialize(codec.serialize(root));

也可以递归调用

http://www.cnblogs.com/yrbbest/p/5047035.html

别人的答案

https://discuss.leetcode.com/topic/30195/recursive-dfs-iterative-dfs-and-bfs

更多讨论

https://discuss.leetcode.com/category/375/serialize-and-deserialize-binary-tree

以上是关于297.Serialize and Deserialize Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree

297.Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree