[LeetCode][JavaScript]Serialize and Deserialize Binary Tree

Serialize and Deserialize Binary Tree

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.

For example, you may serialize the following tree

    1
   / \
  2   3
     / \
    4   5
as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

 

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

https://leetcode.com/problems/serialize-and-deserialize-binary-tree/

 

 

 


 

 

串行化和反串行化树。

串行化,中序遍历树,如果left或者right没有孩子,就用N代替,用#连接各个节点。

比图题中的例子,串行化之后是1#2#N#N#3#4#N#N#5#N#N。

反串行化还是中序遍历,递归建出树。

LDR方法有2个返回值,node是当前的节点对象,i是nodes下标。

 

 1 /**
 2  * Definition for a binary tree node.
 3  * function TreeNode(val) {
 4  *     this.val = val;
 5  *     this.left = this.right = null;
 6  * }
 7  */
 8 /**
 9  * Encodes a tree to a single string.
10  *
11  * @param {TreeNode} root
12  * @return {string}
13  */
14 var serialize = function(root) {
15     var res = "";
16     LDR(root);
17     if(res[0] === '#'){
18         res = res.substring(1);
19     }
20     return res;
21 
22     function LDR(node){
23         if(node !== null && node.val !== undefined){
24             res += "#" + node.val;
25             if(node.left !== null){
26                 LDR(node.left);
27             }else{
28                 res += "#N";
29             }
30             if(node.right !== null){
31                 LDR(node.right);
32             }else{
33                 res += "#N";
34             }
35         }
36     }
37 };
38 
39 /**
40  * Decodes your encoded data to tree.
41  *
42  * @param {string} data
43  * @return {TreeNode}
44  */
45 var deserialize = function(data) {
46     var nodes = data.split('#');
47     return LDR(0).node;
48 
49     function LDR(i){
50         if(nodes[i] !== undefined && nodes[i] !== "" && nodes[i] !== 'N'){
51             var root = new TreeNode(parseInt(nodes[i]));
52             i++;
53             var res = LDR(i);
54             i = res.i;
55             root.left = res.node;
56             res = LDR(i);
57             i = res.i;
58             root.right = res.node;
59             return {node : root, i : i};
60         }else{
61             return {node : null, i : ++i};
62         }
63     }
64 };

 

 

 

 

 

 

 

 

 

 

 

posted @ 2015-10-31 18:30  `Liok  阅读(591)  评论(0编辑  收藏  举报