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 5as
"[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.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
private class MutableInt{
public int val;
public MutableInt(int val){
this.val = val;
}
}
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null)
return null;
StringBuffer result = serializeHelper(root);
return result.toString();
}
private StringBuffer serializeHelper(TreeNode root){
if(root == null)
return new StringBuffer("n");
StringBuffer result = new StringBuffer(String.valueOf(root.val));
result.append(" ");
result.append(serializeHelper(root.left));
result.append(" ");
result.append(serializeHelper(root.right));
return result;
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == null)
return null;
return deserializeHelper(data.split(" "), new MutableInt(0));
}
private TreeNode deserializeHelper(String[] data, MutableInt index){
int indexVal = index.val;
if(data[indexVal].equals("n")){
index.val = indexVal + 1;
return null;
}
TreeNode result = new TreeNode(Integer.parseInt(data[indexVal]));
index.val = indexVal + 1;
result.left = deserializeHelper(data, index);
result.right = deserializeHelper(data, index);
return result;
}
}
按照 tree mining的方法encode,稍加改变可作为N-ary tree encoding.但是每个node的children里若有null,则null children之后的children必须都为null。即不能有null出现在中间
class Codec {
private class MutableInt{
public int val;
public MutableInt(int val){
this.val = val;
}
}
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null)
return null;
StringBuffer result = serializeHelper(root);
return result.toString();
}
private StringBuffer serializeHelper(TreeNode root){
if(root.left == null && root.right == null)
return new StringBuffer(String.valueOf(root.val) + " n");
StringBuffer result = new StringBuffer(String.valueOf(root.val));
result.append(" ");
result.append(serializeHelper(root.left));
if(root.right != null) {
result.append(" ");
result.append(serializeHelper(root.right));
}
result.append(" n");
return result;
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == null)
return null;
return deserializeHelper(data.split(" "), new MutableInt(0));
}
private TreeNode deserializeHelper(String[] data, MutableInt index){
int indexVal = index.val;
TreeNode result = new TreeNode(Integer.parseInt(data[indexVal]));
index.val = indexVal + 1;
if(data[index.val].equals("n")) {
index.val += 1;
return result;
}
result.left = deserializeHelper(data, index);
if(data[index.val].equals("n")) {
index.val += 1;
return result;
}
result.right = deserializeHelper(data, index);
return result;
}
}