2015年9月10日星期四

Lintcode: Binary Tree Serialization

Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.
There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure.
Have you met this question in a real interview? 
Yes
Example
An example of testdata: Binary tree {3,9,20,#,#,15,7}, denote the following structure:
  3
 / \
9  20
  /  \
 15   7
Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.
You can use other method to do serializaiton and deserialization.
Tags Expand 

Reference:
How to use istringstream  // space will automatically separate different parameters
Note: Line 21 to_string() is necessray, string cannot add int directly
use istringstream as input, when reading values, we don't need to consider space


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
 
class Solution {
private:
    void _serialize(TreeNode *root, string& out){
        if(root == NULL) {
            out += "# ";
            return;
        }
        out += to_string(root->val);
        out += " ";
        _serialize(root->left, out);
        _serialize(root->right, out);
    }
    TreeNode* _deserialize(istringstream &in){
        char str[20];
        in >> str;
        if (str[0] == '#') return NULL;
        
        TreeNode *root = new TreeNode(atoi(str));
        root->left = _deserialize(in);
        root->right = _deserialize(in);
        return root;
    }
public:
   
    string serialize(TreeNode *root) {
        // write your code here
        string res;
        _serialize(root, res);
        return res;
    }

    TreeNode *deserialize(string data) {
        // write your code here
        istringstream in(data);
        return _deserialize(in);
    }
};


Lintcode second;
BFS http://www.cnblogs.com/EdwardLiu/p/4391418.html


Total original:
represent the tree by level, all NULL represent by #
Time O(n)
Space O(n)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * This method will be invoked first, you should design your own algorithm 
     * to serialize a binary tree which denote by a root node to a string which
     * can be easily deserialized by your own "deserialize" method later.
     */
    string serialize(TreeNode *root) {
        // write your code here
        string res;
        queue<TreeNode *> q;
        q.push(root);
        while(!q.empty()){
            TreeNode *cur = q.front();
            q.pop();
            if(cur == NULL){
                res += ("# ");
                continue;
            }
            res += to_string(cur->val);
            res += " ",
            q.push(cur->left);
            q.push(cur->right);
        }
        return res;
    }

    /**
     * This method will be invoked second, the argument data is what exactly
     * you serialized at method "serialize", that means the data is not given by
     * system, it's given by your own serialize method. So the format of data is
     * designed by yourself, and deserialize it here as you serialize it in 
     * "serialize" method.
     */
    TreeNode *deserialize(string data) {
        // write your code here
        istringstream in(data);
        char str[20];
        queue<TreeNode *> q;
        in>>str;
        if(str[0] == '#') return NULL;
        TreeNode *head = new TreeNode(atoi(str));
        q.push(head);
        while(!q.empty()){
            TreeNode *cur = q.front();
            q.pop();
            in>>str;
            if(str[0] == '#') cur->left = NULL;
            else {
                cur->left = new TreeNode(atoi(str));
                q.push(cur->left);
            }
            in>>str;
            if(str[0] == '#') cur->right = NULL;
            else{
                cur->right = new TreeNode(atoi(str));
                q.push(cur->right);
            }
        }
        return head;
    }
};

没有评论:

发表评论