Leetcode 449. Serialize and Deserialize BST

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

1. Description

Serialize and Deserialize BST

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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值