Leetcode 449. Serialize and Deserialize BST

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

1. Description

Leetcode 449. Serialize and Deserialize BST_第1张图片

2. Solution

  • First Version
/**
 * 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
/**
 * 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/

你可能感兴趣的:(Algorithm,Leetcode)