leetcode difficulty - serialization and deserialization of binary tree (simple recursion)

(Although I know how to write it at a glance, it took a long time to debug, which is uncomfortable and too good)

Topic:
Serialization is the operation of converting a data structure or object into continuous bits, and then the converted data can be stored in a file or memory, and can also be transmitted to another computer environment through the network, taking the opposite method Reconstruct to get the original data.
Please design an algorithm to realize the serialization and deserialization of the binary tree. This does not limit your sequence/deserialization algorithm execution logic, you only need to ensure that a binary tree can be serialized into a string and deserialize the string into the original tree structure.
Tip: The input and output format is consistent with the method currently used by LeetCode. For details, please refer to LeetCode Serialized Binary Tree Format. You don't have to take this approach, you can use other methods to solve this problem.

Solution:
There are two
kinds of nodes. One is the node whose string is saved as NULL, so that the result of the preorder traversal can be
kept
. Practice your hands, use the second method)

If you use the second method, there are a few small details:
① The index of each node should be preserved, because some different nodes have the same value, so if it is in the preorder and inorder, it is not known whether it is the same value
② Cannot be saved as "Value index value index..." This may match "value index" and match an inexplicable "index value" (has been stepped on)
③ string addition, try to avoid str = str1 + str2, the efficiency is very low, you can use append
(There is a very strange point here, str += str1 is more efficient than str = str + str1, it is estimated that operator overloading optimizes performance)

The code is as follows, no difficulty:

class Codec {
public:

    // Encodes a tree to a single string.
    TreeNode* prev;
    vector<pair<int, int> > temp1;
    vector<pair<int, int> > temp2;
    int flag = 0;
    TreeNode* prep(TreeNode* v) {
        if(v == NULL) return NULL;
        TreeNode* t = new TreeNode(flag);
        flag++;
        t -> left = prep(v -> left);
        t -> right = prep(v -> right);
        return t;
    }
    void solve1(TreeNode* v, TreeNode* t) {
        if(v == NULL) return;
        temp1.push_back(make_pair(v -> val, t -> val));
        solve1(v -> left, t -> left);
        solve1(v -> right, t -> right);
    }
    void solve2(TreeNode* v, TreeNode* t) {
        if(v == NULL) return;
        solve2(v -> left, t -> left);
        temp2.push_back(make_pair(v -> val, t -> val));
        solve2(v -> right, t -> right);
    }
    string serialize(TreeNode* root) {
        string res = "";
        prev = prep(root);
        solve1(root, prev);
        solve2(root, prev);
        for(int i = 0; i < temp1.size(); i++) {
            res += to_string(temp1[i].first) + ' ' + to_string(temp1[i].second) + '!'; // 注意这里字符串拼接效率,不然真会超时
        }
        res = res + '?';
        for(int i = 0; i < temp2.size(); i++) {
            res += to_string(temp2[i].first) + ' ' + to_string(temp2[i].second) + '!'; // 注意这里字符串拼接效率,不然真会超时
        }
        return res;
    }

    // Decodes your encoded data to tree.
    string str1 = "";
    string str2 = "";
    TreeNode* solve(string s1, string s2) {
        if(s1 == "") return NULL;
        int t = s1.find('!') + 1;
        string val = s1.substr(0, t);
        int flag = s2.find(val);
        string s11, s21, s12, s22;
        if(flag == 0) {
            s11 = s21 = "";
        }
        else {
            s11 = s1.substr(val.size(), flag);
            s21 = s2.substr(0, flag);
        }
        if(val.size() + flag == s2.size()) {
            s12 = s22 = "";
        }
        else {
            s12 = s1.substr(val.size() + flag);
            s22 = s2.substr(flag + val.size());
        }
        TreeNode* v = new TreeNode(atoi(val.substr(0, val.find(' ')).c_str()));
        v -> left = solve(s11, s21);
        v -> right = solve(s12, s22);
        return v;
    }
    TreeNode* deserialize(string data) {
        TreeNode* res;
        int t = data.find('?');
        str1 = data.substr(0, t);
        str2 = data.substr(t + 1, t);
        res = solve(str1, str2);
        return res;
    }
};

Guess you like

Origin blog.csdn.net/m0_52212261/article/details/128906971