Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
The number of nodes in the tree is in the range [0, 104].
-1000 <= Node.val <= 1000
ac1: BFS
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassCodec {// Encodes a tree to a single string.publicStringserialize(TreeNode root) {// edge casesif (root ==null) return"null";StringBuilder sb =newStringBuilder();Queue<TreeNode> q =newLinkedList<>();q.offer(root);while (!q.isEmpty()) {TreeNode h =q.poll();if (h ==null) {sb.append("null,"); } else {sb.append(""+h.val+",");q.offer(h.left);q.offer(h.right); } }sb.setLength(sb.length()-1); // remove last ","returnsb.toString(); }// Decodes your encoded data to tree.publicTreeNodedeserialize(String data) {String[] s =data.split(",");if ("null".equals(s[0])) returnnull;TreeNode root =newTreeNode(Integer.parseInt(s[0])); // set rootQueue<TreeNode> q =newLinkedList<>();q.offer(root);int i =1;while(!q.isEmpty()) {TreeNode h =q.poll();String l = s[i++];h.left="null".equals(l) ?null:newTreeNode(Integer.parseInt(l));String r = s[i++];h.right="null".equals(r) ?null:newTreeNode(Integer.parseInt(r));if (h.left!=null) q.offer(h.left);if (h.right!=null) q.offer(h.right); }return root; }}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.deserialize(codec.serialize(root));/*1. BFS2. DFS, pre-order*/
ac2: DFS pre-order
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassCodec {// Encodes a tree to a single string.publicStringserialize(TreeNode root) {StringBuilder sb =newStringBuilder();seri(root, sb);returnsb.toString(); }privatevoidseri(TreeNode node,StringBuilder sb) {// exitif (node ==null) {sb.append("null,");return; }sb.append(""+node.val+",");seri(node.left, sb);seri(node.right, sb); }// Decodes your encoded data to tree.publicTreeNodedeserialize(String data) {Queue<String> q =newLinkedList<>();for (String s :data.split(",")) {q.offer(s); }returndese(q); }privateTreeNodedese(Queue<String> q) {String str =q.poll();TreeNode root ="null".equals(str) ?null:newTreeNode(Integer.parseInt(str));if (root !=null) {root.left=dese(q);root.right=dese(q); }return root; }}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.deserialize(codec.serialize(root));/*1. BFS2. DFS, pre-order*/