序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。

请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。

提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。

示例 1:
image.png

输入:root = [1,2,3,null,null,4,5]
输出:[1,2,3,null,null,4,5]
示例 2:

输入:root = []
输出:[]
示例 3:

输入:root = [1]
输出:[1]
示例 4:

输入:root = [1,2]
输出:[1,2]

提示:

树中结点数在范围 [0, 104] 内
-1000 <= Node.val <= 1000


dfs

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Codec {
  11. // Encodes a tree to a single string.
  12. public String serialize(TreeNode root) {
  13. if (root == null) return "X,";
  14. String left = serialize(root.left);
  15. String right = serialize(root.right);
  16. return root.val + "," + left + right;
  17. }
  18. // Decodes your encoded data to tree.
  19. public TreeNode deserialize(String data) {
  20. //去掉逗号[1 | 2 X X | 3 4 XX 5 XX ]
  21. String[] ss = data.split(",");
  22. //队列来存
  23. Deque<String> q = new LinkedList<>(Arrays.asList(ss));
  24. return dfs(q);
  25. }
  26. TreeNode dfs(Deque<String> q) {
  27. String s = q.pollFirst();
  28. if (s.equals("X")) return null;
  29. TreeNode root = new TreeNode(Integer.parseInt(s));
  30. root.left = dfs(q);
  31. root.right = dfs(q);
  32. return root;
  33. }
  34. }
  35. // Your Codec object will be instantiated and called as such:
  36. // Codec ser = new Codec();
  37. // Codec deser = new Codec();
  38. // TreeNode ans = deser.deserialize(ser.serialize(root));

bfs

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Codec {
  11. //bfs做法
  12. // Encodes a tree to a single string.
  13. public String serialize(TreeNode root) {
  14. if (root == null) return "";
  15. Deque<TreeNode> q = new LinkedList<>();
  16. //前序遍历
  17. q.addLast(root);
  18. StringBuilder res = new StringBuilder();
  19. while (!q.isEmpty()) {
  20. TreeNode node = q.pollFirst();
  21. if (node == null) {
  22. res.append("x,");
  23. continue;
  24. }
  25. res.append(node.val + ",");
  26. q.addLast(node.left);
  27. q.addLast(node.right);
  28. }
  29. return res.toString();
  30. }
  31. // Decodes your encoded data to tree.
  32. public TreeNode deserialize(String data) {
  33. if (data == "") return null;
  34. //层序遍历
  35. String[] ss = data.split(",");
  36. //队列来存
  37. Deque<String> q = new LinkedList<>(Arrays.asList(ss));
  38. Deque<TreeNode> queue = new LinkedList<>();
  39. TreeNode res = new TreeNode(Integer.parseInt(q.pollFirst()));
  40. queue.addLast(res);
  41. while (!queue.isEmpty()) {
  42. TreeNode node = queue.pollFirst();
  43. String left = q.pollFirst(), right = q.pollFirst();
  44. if (!left.equals("x")) {
  45. node.left = new TreeNode(Integer.parseInt(left));
  46. queue.addLast(node.left);
  47. }
  48. if (!right.equals("x")) {
  49. node.right = new TreeNode(Integer.parseInt(right));
  50. queue.addLast(node.right);
  51. }
  52. }
  53. return res;
  54. }
  55. }
  56. // Your Codec object will be instantiated and called as such:
  57. // Codec ser = new Codec();
  58. // Codec deser = new Codec();
  59. // TreeNode ans = deser.deserialize(ser.serialize(root));