297. Serialize and Deserialize Binary Tree

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
/*[1,2,3,1,3,2,4]  Runtime Error */
/*
class Codec {
public:


// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string str = "";
inOrder(root, str);
if (str == "") return "";
str.pop_back();
str.push_back(';');


postOrder(root, str);
str.pop_back();


return str;
}


// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
string str(data);
//if (str == ";") return NULL;
if (str == "") return NULL;
string strInOrder;
string strPostOrder;
string::iterator it;
it = std::find(str.begin(), str.end(), ';');
strInOrder = str.substr(0, it - str.begin());
strPostOrder = str.substr(it - str.begin() + 1, str.size() - 1 - strInOrder.size());


vector<int> vInOrder;
vector<int> vPostOrder;
stringToVector(strInOrder, vInOrder, ',');
stringToVector(strPostOrder, vPostOrder, ',');


return createTree(vInOrder, vPostOrder);
}


private:
void inOrder(TreeNode* root, string& str)
{
if (NULL == root) return;
if (root->left)
{
inOrder(root->left, str);
}
str += std::to_string(root->val);
str += ",";
if (root->right)
{
inOrder(root->right, str);
}
}
void postOrder(TreeNode* root, string& str)
{
if (NULL == root) return;
if (root->left)
{
postOrder(root->left, str);
}
if (root->right)
{
postOrder(root->right, str);
}
str += std::to_string(root->val);
str += ",";


}


int parseInt(string& str)
{
int ans = 0;
for (char c : str)
{
ans += 10 * ans + (c - '0');
}


return ans;
}


void stringToVector(string& str, vector<int>& list, char septor)
{
for (int i = 0; i<str.size();)
{
int j = i;
while (str[j] != septor && j<str.size()) ++j;
string strTemp;
strTemp = str.substr(i, j - i);
list.push_back(parseInt(strTemp));
i = j + 1;
}
}


TreeNode* createTree(vector<int>& nums1, vector<int>& nums2)
{
//nums1-inorder
//nums2-postorder
if (nums2.size() == 0) return NULL;
TreeNode* root;
root = new TreeNode((nums2.back()));
//nums2.pop_back();

//if (nums2.size() == 0) return root;
vector<int>::iterator it = std::find(nums1.begin(), nums1.end(), root->val);
vector<int> newNum1 = vector<int>(nums1.begin(), it);
vector<int> newNum2 = vector<int>(nums2.begin(), newNum1.size() + nums2.begin());


root->left = createTree(newNum1, newNum2);

newNum1 = vector<int>(it + 1, nums1.end());
newNum2 = vector<int>(nums2.end()-newNum1.size()-1,nums2.end()-1);
root->right = createTree(newNum1, newNum2);


return root;
}
};
*/


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


// Acception of others ! should study in detail!
class Codec {
public:


    string serialize(TreeNode* root) {
        ostringstream out;
        serialize(root, out);
        return out.str();
    }


    TreeNode* deserialize(string data) {
        istringstream in(data);
        return deserialize(in);
    }


private:


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


    TreeNode* deserialize(istringstream& in) {
        string val;
        in >> val;
        if (val == "#")
            return nullptr;
        TreeNode* root = new TreeNode(stoi(val));
        root->left = deserialize(in);
        root->right = deserialize(in);
        return root;
    }
};

猜你喜欢

转载自blog.csdn.net/bjzhaoxiao/article/details/80178123