import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Codec {
/**
* 利用前序遍历递归实现
*/
public static String serialize(TreeNode root) {
LinkedList<String> queue = new LinkedList<>();
ser(root, queue);
StringBuilder builder = new StringBuilder();
Iterator<String> iterator = queue.iterator();
while (true) {
String next = iterator.next();
builder.append(next);
if (!iterator.hasNext()) {
break;
}
builder.append(",");
}
return builder.toString();
}
public static TreeNode deserialize(String data) {
LinkedList<String> queue = new LinkedList<>(Arrays.asList(data.split(",")));
return deser(queue);
}
public static void ser(TreeNode root, List<String> queue) {
if (root == null) {
queue.add(null);
return;
}
queue.add(String.valueOf(root.val));
ser(root.left, queue);
ser(root.right, queue);
}
public static TreeNode deser(LinkedList<String> queue) {
String curr = queue.poll();
if (curr.equals("null")) {
return null;
}
TreeNode root = new TreeNode(Integer.parseInt(curr));
root.left = deser(queue);
root.right = deser(queue);
return root;
}
/**
* 通过层次遍历实现
*/
// Encodes a tree to a single string.
public static String serialize2(TreeNode root) {
if (root == null) {
return null;
}
LinkedList<Integer> list = new LinkedList<>();
list.add(root.val);
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode curr = queue.poll();
if (curr.left != null) {
queue.offer(curr.left);
list.add(curr.left.val);
} else {
list.add(null);
}
if (curr.right != null) {
queue.offer(curr.right);
list.add(curr.right.val);
} else {
list.add(null);
}
}
Iterator<Integer> descendingIterator = list.descendingIterator();
while (descendingIterator.hasNext()) {
Integer curr = descendingIterator.next();
if (curr == null) {
descendingIterator.remove();
} else {
break;
}
}
StringBuilder builder = new StringBuilder();
Iterator<Integer> iterator = list.iterator();
while (true) {
Integer curr = iterator.next();
builder.append(curr);
if (!iterator.hasNext()) {
break;
}
builder.append(",");
}
return builder.toString();
}
// Decodes your encoded data to tree.
public static TreeNode deserialize2(String data) {
if (data == null) {
return null;
}
String[] nums = data.split(",");
LinkedList<TreeNode> queue = new LinkedList<>();
TreeNode root = new TreeNode(Integer.parseInt(nums[0]));
queue.offer(root);
int index = 1;
while (!queue.isEmpty()) {
TreeNode curr = queue.poll();
if (index >= nums.length) {
break;
}
if (!nums[index].equals("null")) {
TreeNode left = new TreeNode(Integer.parseInt(nums[index]));
curr.left = left;
queue.offer(left);
}
index++;
if (index >= nums.length) {
break;
}
if (!nums[index].equals("null")) {
TreeNode right = new TreeNode(Integer.parseInt(nums[index]));
curr.right = right;
queue.offer(right);
}
index++;
}
return root;
}
public static void main(String[] args) {
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
n1.left = n2;
n1.right = n3;
n3.left = n4;
n3.right = n5;
System.out.println(serialize(n1));
TreeNode root = deserialize(serialize(n1));
System.out.println(root);
}
}
二叉树序列化和反序列化
最新推荐文章于 2022-06-20 21:03:52 发布