Leetcode 449

Time:2021-6-11

Portal: https://leetcode-cn.com/probl…
When learning binary trees, there is a property that
If the preorder sequence and the middle sequence of traversing binary tree are given, then a unique binary tree can be constructed
So for this problem, the general idea can be solved according to this property. Because the condition of the problem is “balanced binary tree”, the process of solving the problem can be further simplified as follows:
For each subtree, the serialization string is s,

  • If the substring s [I1… In] > s [0] can be found, then s is divided into left subtree and right subtree according to I1;
  • If such substrings cannot be found, they are all in the left subtree;
  • Correspondingly, if I1 = 1, they are all on the right subtree.

Recurses the above procedure to deserialize the serialized string.
Due to the structure of the tree, each node is separated by ‘,’during serialization.

class Codec {
public:
    string serialize(TreeNode* root) {
        string result;
        getFront(root,result);
        return result;
    }
    void getFront(TreeNode* root,string &result){
        if(!root) return;
        result+=to_string(root->val)+",";
        if(root->left){getFront(root->left,result);}
        if(root->right){getFront(root->right,result);}
    }

    TreeNode* deserialize(string data) {
        if(data.length()==0||data.length()==1) return 0;
        TreeNode *result = new TreeNode;
        int i=0;result->val = 0;
        for(;data[i]!=','&&i<data.length();++i){
            result->val = result->val * 10 + data[i]-'0';
        }
        //Solve s [I1... In], begin points to the first character, end points to the last ','
        string left,right;int start = ++i;int end = i-1;
        for(;i<data.length();++i){
            int tmp = 0;
            for(;data[i]!=',';++i){
                tmp=tmp * 10 + data[i]-'0';
            }
            if(tmp>result->val) break;
            else end = i;
        }
        if(start<end){
            left = data.substr(start,end-start+1);
            TreeNode *nl = deserialize(left);
            if(nl) result->left = nl;
        }
        if(end!=data.length()-1){
            right = data.substr(end+1,data.length()-end);
            TreeNode *nr = deserialize(right);
            if(nr) result->right = nr;
        }
        return result;
    }
};

Leetcode 449
The effect is average
Give it a trylevel traversal
The serialization string is generated according to the hierarchy traversal. If the node is empty, spaces are generated instead of numbers.
The difficulty is to regenerate the tree. It’s easy to understand that since the position of the empty node is also reserved when generating the string, when deserializing, the root node is traversed again, and the position of each point in the serialized string is corresponding.
Again, traverse the hierarchy

TreeNode* deserialize(string data) {
        if(data.length()==0) return 0x00;
        TreeNode *result = new TreeNode();
        queue<TreeNode*> que;
        que.push(result);
        for(int i=0;i<data.length();++i){
            int tmp = 0;
            TreeNode *tmpN = que.front();
            que.pop();
            if(data[i]==' '){
                if(tmpN->left)  tmpN->left->left = 0x00;
                if(tmpN->right) tmpN->right->right = 0x00;
                ++i;continue;
            }
            for(;data[i]!=',';++i) tmp = tmp*10 + data[i]-'0';
            tmpN->val = tmp;
            //We need to allocate space here. When it is pushed into the queue and taken out, the space is reallocated and its parent node cannot be connected
            tmpN->left  = 0x00;tmpN->left = new TreeNode();
            //If the left node is borrowed, the parent node can be found and the left subtree of the parent node can be left empty. Same as right node
            tmpN->left->left=tmpN;
            que.push(tmpN->left);
            tmpN->right = 0x00;tmpN->right = new TreeNode();
            tmpN->right->right=tmpN; 
            que.push(tmpN->right);
        }
        return  result;
    }

It turned out to be better.
Leetcode 449

297 is the same as that:https://leetcode-cn.com/probl…
Only need to use the method of hierarchical traversal (because this problem is a binary tree, unbalanced tree, method 1 is not applicable)
Just judge + – once