[LeetCode] 297. Serialize and Deserialize Binary Tree

Serialization and de-serialization binary tree. Casual working that is the meaning of the questions. example,

Example: 

You may serialize the following tree:

    1
   / \
  2   3
     / \
    4   5

as "[1,2,3,null,null,4,5]"

The idea is to BFS. First serialized. Nodes in the tree along the lines of level order traversal. If the current node is NULL then into "null"; if not, the normal traversal of the current node left child and right child.

Deserialization is to convert a character string input into a tree structure. First the string to get the first node, as the root node. After traversing the input or ideas according to BFS, if not for the "null" is made into a node and join queue.

Time O (n)

Space O (n)

Java implementation

 1 public class Codec {
 2 
 3     // Encodes a tree to a single string.
 4     public String serialize(TreeNode root) {
 5         if (root == null)
 6             return "";
 7         StringBuilder res = new StringBuilder();
 8         Queue<TreeNode> queue = new LinkedList<>();
 9         queue.offer(root);
10         while (!queue.isEmpty()) {
11             TreeNode cur = queue.poll();
12             if (cur == null) {
13                 res.append("null ");
14                 continue;
15             }
16             res.append(cur.val + " ");
17             queue.offer(cur.left);
18             queue.offer(cur.right);
19         }
20         return res.toString();
21     }
22 
23     // Decodes your encoded data to tree.
24     public TreeNode deserialize(String data) {
25         if (data == "")
26             return null;
27         String[] str = data.split(" ");
28         TreeNode root = new TreeNode(Integer.parseInt(str[0]));
29         Queue<TreeNode> queue = new LinkedList<>();
30         queue.offer(root);
31         for (int i = 1; i < str.length; i++) {
32             TreeNode cur = queue.poll();
33             if (!str[i].equals("null")) {
34                 cur.left = new TreeNode(Integer.parseInt(str[i]));
35                 queue.offer(cur.left);
36             }
37             i++;
38             if (!str[i].equals("null")) {
39                 cur.right = new TreeNode(Integer.parseInt(str[i]));
40                 queue.offer(cur.right);
41             }
42         }
43         return root;
44     }
45 }
46 
47 // Your Codec object will be instantiated and called as such:
48 // Codec codec = new Codec();
49 // codec.deserialize(codec.serialize(root));

 

JavaScript implementation

 1 /**
 2  * Encodes a tree to a single string.
 3  *
 4  * @param {TreeNode} root
 5  * @return {string}
 6  */
 7 var serialize = function (root) {
 8     let stack = [];
 9     let serialize = [];
10     if (root == null) return [];
11     stack.push(root);
12     while (stack.length > 0) {
13         let cur = stack.shift();
14         serialize.push(cur ? cur.val : null);
15         if (cur != null) {
16             stack.push(cur.left);
17             stack.push(cur.right);
18         }
19     }
20     return serialize;
21 };
22 
23 var deserialize = function (data) {
24     if (data[0] == null) return null;
25     let node = new TreeNode(data.shift());
26     let queue = [node];
27     while (queue.length > 0) {
28         let node = queue.shift();
29         left = data.shift();
30         right = data.shift();
31         node.left = (left == null) ? null : new TreeNode(left);
32         node.right = (right == null) ? null : new TreeNode(right);
33         if (node.left != null) {
34             queue.push(node.left);
35         }
36         if (node.right != null) {
37             queue.push(node.right);
38         }
39     }
40     return node;
41 };
42 
43 /**
44  * Your functions will be called as such:
45  * deserialize(serialize(root));
46  */

 

Guess you like

Origin www.cnblogs.com/aaronliu1991/p/12453308.html