源码可直接在eclipse中运行
实现方法时运用队列 进行序列化 和 反序列化
嫌麻烦的小伙伴可以采用DFS的方法 写起来比较简洁
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Codec {
public static void main(String[] args) {
// TODO Auto-generated method stub
Codec codec = new Codec();
//codec.myPrint(codec.deserialize("[1,2,null,null,3,4,null,null,5,null,null,]"));
System.out.println(codec.serialize(codec.deserialize("[1,2,null,null,3,4,null,null,5,null,null,]")));
}
//public void myPrint(TreeNode root) {//测试反序列化效果
// if (root==null) return;
// System.out.println(root.val);
// myPrint(root.left);
// myPrint(root.right);
//}
//将二叉树序列化 采用队列的形式
public String serialize(TreeNode root) {
if (root==null) return "[]";
StringBuilder res=new StringBuilder();
Queue<TreeNode> queue = new LinkedList<>();
if (root != null) {
queue.add(root);
res.append(Integer.toString(root.val));
}
while (!queue.isEmpty()) {
int n = queue.size();
for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
if (node.left!=null) {
queue.add(node.left);
res.append("," ).append(Integer.toString(node.left.val));
}
else
res.append(",null");
if (node.right!=null) {
queue.add(node.right);
res.append(",").append(Integer.toString(node.right.val));
}
else
res.append(",null");
}
}
char [] ans=res.toString().toCharArray();
int i=ans.length-1;
while (ans[i]<48 || ans[i]>57) {
i--;
}
StringBuilder sb=new StringBuilder();
for (int j=0;j<i+1;j++)
{
sb.append(ans[j]);
}
return "[" + sb + "]";
}
//反序列化
public TreeNode deserialize(String data) {
if ("[]".equals(data)) return null;
data = data.substring(1, data.length() - 1);
String[] datas = data.split(",");
TreeNode root=new TreeNode(Integer.valueOf(datas[0]));
int len=datas.length;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int i=1;
while (!queue.isEmpty())
{
TreeNode node=queue.poll();
if (i>len-1 || "null".equals(datas[i])) {
node.left=null;
}
else {
node.left=new TreeNode(Integer.valueOf(datas[i]));
queue.add(node.left);
}
i++;
if (i>len-1 ||"null".equals(datas[i])) {
node.right=null;
}
else {
node.right=new TreeNode(Integer.valueOf(datas[i]));
queue.add(node.right);
}
i++;
}
return root;
}
}
DFS方法 采用前序遍历的方法进行序列化
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Codec {
public static void main(String[] args) {
// TODO Auto-generated method stub
Codec codec = new Codec();
codec.myPrint(codec.deserialize("[1,2,null,null,3,4,null,null,5,null,null,]"));
System.out.println(codec.serialize(codec.deserialize("[1,2,null,null,3,4,null,null,5,null,null,]")));
}
//前序遍历树
public void recurPreSerialize(TreeNode node,StringBuilder sb){
if (node == null) {
sb.append("null").append(",");
return;
}
//先打印当前节点
sb.append(node.val).append(",");
//打印左子树
recurPreSerialize(node.left,sb);
//打印右子树
recurPreSerialize(node.right,sb);
}
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
recurPreSerialize(root,sb);
return "["+sb.toString()+"]";
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.length()==2) return null;
data = data.substring(1, data.length() - 2);
String[] valus = data.split(",");
List<String> data_list = new LinkedList<String>(Arrays.asList(valus));
return recurDeserialize(data_list);
}
public TreeNode recurDeserialize(List<String> list){
String nodeString = list.remove(0);
if (nodeString.equals("null")) {
return null;
}
TreeNode node = new TreeNode(Integer.parseInt(nodeString));
node.left = recurDeserialize(list);
node.right = recurDeserialize(list);
return node;
}
}