LC.P297[二叉树的序列化与反序列化]

题目描述

序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。

请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。

提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。

示例1

输入:root = [1,2,3,null,null,4,5]
输出:[1,2,3,null,null,4,5]

示例2

输入:root = []
输出:[]

提示:

  • 树中结点数在范围 [0, 104]
  • -1000 <= Node.val <= 1000

方法一:BFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {

private static final String EMPTY = "NULL";

// BFS
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) return "";
StringBuilder builder = new StringBuilder();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node == null) builder.append(EMPTY + ",");
else {
builder.append(node.val).append(",");
queue.offer(node.left);
queue.offer(node.right);
}
}
return builder.toString();
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data == "") return null;
String[] split = data.split(",");
Queue<String> nodes = new LinkedList<>(Arrays.asList(split));
TreeNode root = new TreeNode(Integer.parseInt(nodes.poll()));
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
String left = nodes.poll();
String right = nodes.poll();
if (!left.equals(EMPTY)) {
node.left = new TreeNode(Integer.parseInt(left));
queue.offer(node.left);
}
if (!right.equals(EMPTY)) {
node.right = new TreeNode(Integer.parseInt(right));
queue.offer(node.right);
}
}
return root;
}
}

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));

方法二:DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {

private static final String EMPTY = "NULL";

// DFS
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) {
return EMPTY + ",";
}
String left = serialize(root.left);
String right = serialize(root.right);
return root.val + "," + left + right;
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] nodes = data.split(",");
Queue<String> queue = new LinkedList<>(Arrays.asList(nodes));
return buildTree(queue);
}

public TreeNode buildTree(Queue<String> queue) {
String value = queue.poll();
if (value.equals(EMPTY)) {
return null;
}
TreeNode node = new TreeNode(Integer.parseInt(value));
node.left = buildTree(queue);
node.right = buildTree(queue);
return node;
}
}

// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));