同时这一题也是Leetcode的297题,二叉树的序列化和反序列化
题目描述
请实现两个函数,分别用来序列化和反序列化二叉树
二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存。序列化可以基于先序、中序、后序、层序的二叉树遍历方式来进行修改,序列化的结果是一个字符串,序列化时通过 某种符号表示空节点(#),以 ! 表示一个结点值的结束(value!)。
二叉树的反序列化是指:根据某种遍历顺序得到的序列化字符串结果str,重构二叉树。
思路
写了两种方法的,一个是先序遍历,一个是层次遍历
1.先序遍历
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
private String res = new String("");
String Serialize(TreeNode root) {
//先序遍历
if(root == null){
return "#!";
}
res = String.valueOf(root.val)+"!";
res += Serialize(root.left);
res += Serialize(root.right);
return res;
}
private int index = 0;
TreeNode Deserialize(String str) {
String[] treearray = str.split("!");
TreeNode root = new TreeNode(0);
return Deserialize(treearray,root);
}
TreeNode Deserialize(String[] nums,TreeNode root){
if(index < nums.length){
if(nums[index].equals("#")){
index++;
return null;
}else{
root = new TreeNode(Integer.valueOf(nums[index]));
index++;
root.left = Deserialize(nums,root.left);
root.right = Deserialize(nums,root.right);
return root;
}
}
return null;
}
}
2.层次遍历
但是时间复杂度要比先序遍历的低
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
//层次遍历
if(root == null)
return "[null]";
String res = "[";
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(!queue.isEmpty()){
TreeNode knode = queue.poll();
if(knode != null){
queue.add(knode.left);
queue.add(knode.right);
res += String.valueOf(knode.val) + ",";
}else{
res += "null,";
}
}
res = res.substring(0,res.length()-1);
res += "]";
return res;
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
System.out.println(data);
data = data.substring(1,data.length()-1);
String[] strs = data.split(",");
if(strs[0].equals("null")){
return null;
}
/*Queue<TreeNode> queue = new LinkedList<>();
for(int i = 0;i < strs.length;i ++){
if(strs[i].equals("null")){
queue.add(null);
}else{
queue.add(new TreeNode(Integer.valueOf(strs[i])));
}
}*/
Queue<TreeNode> tmp = new LinkedList<>();
TreeNode root = new TreeNode(Integer.valueOf(strs[0]));
TreeNode prootoftree = root;
int i = 1;
//while(!queue.isEmpty()){
while(i < strs.length){
if(!strs[i].equals("null")){
root.left = new TreeNode(Integer.valueOf(strs[i]));
tmp.add(root.left);
}
if(!strs[i+1].equals("null")){
root.right = new TreeNode(Integer.valueOf(strs[i+1]));
tmp.add(root.right);
}
i = i+2;
root = tmp.poll();//这里层次遍历太妙了
}
return prootoftree;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
数组转换成链表。
List kk = new LinkedList<>(Arrays.asList(strs));