地址:https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/
Java 代码:
import java.util.LinkedList;
import java.util.Queue;
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) {
return "";
}
StringBuilder stringBuilder = new StringBuilder();
dfs(root, stringBuilder);
return stringBuilder.toString();
}
private void dfs(TreeNode node, StringBuilder stringBuilder) {
if (node == null) {
stringBuilder.append("#").append(",");
return;
}
stringBuilder.append(node.val).append(",");
dfs(node.left, stringBuilder);
dfs(node.right, stringBuilder);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
// 特判
if (data.length() == 0) {
return null;
}
// 特别要注意,这个方法在 "" 字符串的时候,会返回数组 [""]
String[] split = data.split(",");
Queue<String> queue = new LinkedList<>();
for (String str : split) {
queue.offer(str);
}
return dfs(queue);
}
private TreeNode dfs(Queue<String> queue) {
if (queue.isEmpty()) {
return null;
}
String head = queue.poll();
if ("#".equals(head)) {
return null;
}
TreeNode root = new TreeNode(Integer.parseInt(head));
root.left = dfs(queue);
root.right = dfs(queue);
return root;
}
}