题目描述
题目链接
方法思路
Approach1:
The idea is simple: print the tree in pre-order traversal and use “X” to denote null node and split node with “,”. We can use a StringBuilder for building the string on the fly. For deserializing, we use a Queue to store the pre-order traversal and since we have “X” as null node, we know exactly how to where to end building subtress.
public class Codec {
//Runtime: 10 ms, faster than 87.87%
//Memory Usage: 39.3 MB, less than 75.73%
private static final String spliter = ",";
private static final String NN = "X";
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
buildString(root, sb);
return sb.toString();
}
private void buildString(TreeNode node, StringBuilder sb) {
if (node == null) {
sb.append(NN).append(spliter);
} else {
sb.append(node.val).append(spliter);
buildString(node.left, sb);
buildString(node.right,sb);
}
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
Deque<String> nodes = new LinkedList<>();
nodes.addAll(Arrays.asList(data.split(spliter)));
return buildTree(nodes);
}
private TreeNode buildTree(Deque<String> nodes) {
String val = nodes.remove();
if (val.equals(NN)) return null;
else {
TreeNode node = new TreeNode(Integer.parseInt(val));
node.left = buildTree(nodes);
node.right = buildTree(nodes);
return node;
}
}
}
Approach2: 层序遍历
public class Codec {
//Runtime: 13 ms, faster than 57.54%
//Memory Usage: 38.5 MB, less than 89.87%
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node != null) {
sb.append(node.val);
queue.offer(node.left);
queue.offer(node.right);
} else {
sb.append("null");
}
sb.append(",");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] vals = data.split(",");
TreeNode[] nodes = new TreeNode[vals.length];
for (int i = 0; i < vals.length; i++) {
nodes[i] = createNode(vals[i]);
}
int parent = 0;
boolean isLeft = true;
for (int i = 1; i < vals.length; i++) {
while (nodes[parent] == null) {
parent++;
}
if (isLeft) {
nodes[parent].left = nodes[i];
isLeft = false;
} else {
nodes[parent].right = nodes[i];
isLeft = true;
parent++;
}
}
return nodes[0];
}
private TreeNode createNode(String s) {
if (s.equals("null")) {
return null;
}
return new TreeNode(Integer.valueOf(s));
}
}
Approach3:
public class Codec {
//Runtime: 50 ms, faster than 18.88%
//Memory Usage: 42.2 MB, less than 18.13%
// Encodes a tree to a single string.
public String serialize(TreeNode root)
{
if(root == null) return "#";
return "" + root.val + " " + serialize(root.left) + " " + serialize(root.right);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data)
{
return build(new Scanner(data));
}
private TreeNode build(Scanner sc)
{
if(!sc.hasNext()) return null;
String tk = sc.next();
if(tk.equals("#")) return null;
TreeNode root = new TreeNode(Integer.parseInt(tk));
root.left = build(sc);
root.right = build(sc);
return root;
}
}