My LeetCode: https://leetcode-cn.com/u/ituring/
My LeetCode source code [GitHub]: https://github.com/izhoujie/Algorithmcii
LeetCode 297. Serialization and deserialization of binary tree
subject
Serialization is the operation of converting a data structure or object into continuous bits, and then the transformed data can be stored in a file or memory. At the same time, it can also be transferred to another computer environment through the network, and the original data can be reconstructed in the opposite way.
Please design an algorithm to realize the serialization and deserialization of binary tree. There is no limit to the execution logic of your sequence / deserialization algorithm. You only need to ensure that a binary tree can be serialized into a string and the string can be deserialized into the original tree structure.
__Example:__
You can change the following binary tree: 1 / \ 2 3 / \ 4 5 Serialize to '[1,2,3,null,null,4,5]'
Tip: This is consistent with the way LeetCode is currently used. For details, see the format of LeetCode serialization binary tree. You don't have to do this, you can do it in other ways.
Note: do not use class members / global / static variables to store state. Your serialization and deserialization algorithm should be stateless.
Source: LeetCode
Link: https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree
Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.
Solutions to problems
The example only provides a demonstration. The data format and deserialization logic of the actual serialization do not need to be consistent with the example, as long as the two methods to ensure the implementation can make the data successfully transform each other;
Idea 1-BFS / breadth first / tree level traversal
Serialization: serialize the tree layer by layer, serialize the null value to "null", and splice it with "," p ";
Deserialization: use "," to get the array, the first value is the root node of the tree, and then arrange the left and right child nodes for the tree, which is still restored according to the logic of BFS;
Algorithm complexity:
- Time complexity: ${\ color{Magenta}{\Omicron\left(n\right)}}}$
- Spatial complexity: ${\ color{Magenta}{\Omicron\left(n\right)}}}$
Idea 2-DFS / depth first / tree first order traversal
Serialization: the tree is serialized directly in the order of traversal;
Deserialization: reverse recursively restore the tree, paying attention to the restore order of left and right subtrees;
Algorithm complexity:
- Time complexity: ${\ color{Magenta}{\Omicron\left(n\right)}}}$
- Spatial complexity: ${\ color{Magenta}{\Omicron\left(n\right)}}}$
Algorithm source code example
package leetcode; import java.util.Arrays; import java.util.LinkedList; import java.util.Objects; /** * @author ZhouJie * @date 2020 11:42:01 p.m., May 3, 2010 * @Description: 297. Serialization and deserialization of binary tree * */ public class LeetCode_0297 { } //Definition for a binary tree node. class TreeNode_0297 { int val; TreeNode_0297 left; TreeNode_0297 right; TreeNode_0297(int x) { val = x; } } /** * @author ZhouJie * @date 2020 May 3, 2010 10:48:39 PM * @Description: 1-Hierarchical traversal / BFS, using LinkedList to record nodes; * */ class Codec_297_1 { // Encodes a tree to a single string. public String serialize(TreeNode_0297 root) { if (root == null) { return ""; } LinkedList<TreeNode_0297> queue = new LinkedList<TreeNode_0297>(); StringBuilder sb = new StringBuilder("["); queue.offer(root); while (!queue.isEmpty()) { TreeNode_0297 node = queue.poll(); if (node != null) { sb.append(node.val); queue.offer(node.left); queue.offer(node.right); } else { sb.append("null"); } sb.append(","); } return sb.deleteCharAt(sb.length() - 1).append("]").toString(); } // Decodes your encoded data to tree. public TreeNode_0297 deserialize(String data) { if (data == null || data.isEmpty()) { return null; } String[] nodeArray = data.substring(1, data.length() - 1).split(","); int i = 0; TreeNode_0297 root = buildeNode(nodeArray[i++]); LinkedList<TreeNode_0297> queue = new LinkedList<TreeNode_0297>(); queue.offer(root); while (!queue.isEmpty()) { TreeNode_0297 node = queue.poll(); node.left = buildeNode(nodeArray[i++]); node.right = buildeNode(nodeArray[i++]); if (node.left != null) { queue.offer(node.left); } if (node.right != null) { queue.offer(node.right); } } return root; } private TreeNode_0297 buildeNode(String s) { if (Objects.equals(s, "null")) { return null; } else { return new TreeNode_0297(Integer.valueOf(s)); } } } /** * @author ZhouJie * @date 2020 11:24:23 p.m., May 3, 2010 * @Description: 2-DFS * */ class Codec_297_2 { // Encodes a tree to a single string. public String serialize(TreeNode_0297 root) { return serialize(root, new StringBuilder()); } private String serialize(TreeNode_0297 root, StringBuilder sb) { if (root == null) { sb.append("null,"); } else { sb.append(root.val).append(","); serialize(root.left, sb); serialize(root.right, sb); } return sb.toString(); } // Decodes your encoded data to tree. public TreeNode_0297 deserialize(String data) { return deserialize(new LinkedList<String>(Arrays.asList(data.split(",")))); } private TreeNode_0297 deserialize(LinkedList<String> list) { String s = list.get(0); list.remove(0); if (Objects.equals(s, "null")) { return null; } else { TreeNode_0297 root = buildeNode(s); root.left = deserialize(list); root.right = deserialize(list); return root; } } private TreeNode_0297 buildeNode(String s) { if (Objects.equals(s, "null")) { return null; } else { return new TreeNode_0297(Integer.valueOf(s)); } } } //Your Codec object will be instantiated and called as such: //Codec codec = new Codec(); //codec.deserialize(codec.serialize(root));