Leetcode 449. Serialize and Deserialize BST

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Serialize and Deserialize BST

2. Solution

  • First Version

    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
    /**
    * Definition for a binary tree node.
    * struct TreeNode {
    * int val;
    * TreeNode *left;
    * TreeNode *right;
    * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    * };
    */
    class Codec {
    public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
    if(!root) {
    return "";
    }
    string str;
    inorder(root, str);
    return str;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
    if(data.empty()) {
    return NULL;
    }
    int pos = 0;
    return reconstruct(data, pos, INT_MIN, INT_MAX);
    }

    private:
    void pushIntToString(string& str, int val) {
    char buffer[4];
    memcpy(buffer, &val, sizeof(int));
    for(int i = 0; i < sizeof(int); i++) {
    str.push_back(buffer[i]);
    }
    }


    void inorder(TreeNode* root, string& nodes) {
    if(!root) {
    return;
    }
    pushIntToString(nodes, root->val);
    inorder(root->left, nodes);
    inorder(root->right, nodes);
    }


    TreeNode* reconstruct(const string& str, int& pos,int minValue, int maxValue) {
    if(pos >= str.size()) {
    return NULL;
    }
    int val = 0;
    memcpy(&val, &str[pos], sizeof(int));
    if(val < minValue || val > maxValue) {
    return NULL;
    }
    pos += sizeof(int);
    TreeNode* node = new TreeNode(val);
    node->left = reconstruct(str, pos, minValue, val);
    node->right = reconstruct(str, pos, val, maxValue);
    return node;
    }
    };

    // Your Codec object will be instantiated and called as such:
    // Codec codec;
    // codec.deserialize(codec.serialize(root));
  • Second Version

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:

// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if(!root) {
return "";
}
ostringstream out;
serialize(root, out);
return out.str();
}

// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(data.empty()) {
return NULL;
}
vector<int> values;
istringstream in(data);
while(in) {
string str;
in >> str;
if(str == "") {
break;
}
values.push_back(stoi(str));
}
int pos = 0;
return deserialize(values, pos, INT_MIN, INT_MAX);
}

private:

void serialize(TreeNode* root, ostringstream& out) {
if(!root) {
return;
}
out << root->val << " ";
serialize(root->left, out);
serialize(root->right, out);
}


TreeNode* deserialize(vector<int>& values,int& pos, int minValue, int maxValue) {
if(pos >= values.size()) {
return NULL;
}
int val = values[pos];
if(val < minValue || val > maxValue) {
return NULL;
}
TreeNode* node = new TreeNode(val);
pos++;
node->left = deserialize(values, pos, minValue, val);
node->right = deserialize(values, pos, val, maxValue);
return node;
}
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

Reference

  1. https://leetcode.com/problems/serialize-and-deserialize-bst/description/
如果有收获,可以请我喝杯咖啡!