题解 | 序列化与反序列化二叉树
序列化二叉树
http://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84
Java层序遍历 使用 # 替代null节点
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
String Serialize(TreeNode root) {
LinkedList<TreeNode> queue = new LinkedList<>();
StringBuffer sb = new StringBuffer();
if (root == null) {
sb.append("#");
return sb.toString();
}
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node == null) {
sb.append(",#");
continue;
}
if (sb.length() != 0) sb.append(",");
sb.append(node.val);
queue.offer(node.left);
queue.offer(node.right);
}
return sb.toString();
}
TreeNode Deserialize(String str) {
String[] items = str.split(",");
if (items[0].equals("#")) return null;
LinkedList<TreeNode> queue = new LinkedList<>();
TreeNode head = new TreeNode(Integer.valueOf(items[0]));
queue.offer(head);
int index = 1;
while (!queue.isEmpty() && index < items.length) {
TreeNode cur = queue.poll();
if (!items[index].equals("#")) {
TreeNode left = new TreeNode(Integer.valueOf(items[index++]));
cur.left = left;
queue.offer(left);
} else {
cur.left = null;
index++;
}
if (!items[index].equals("#")) {
TreeNode right = new TreeNode(Integer.valueOf(items[index++]));
cur.right = right;
queue.offer(right);
} else {
cur.right = null;
index++;
}
}
return head;
}
}