Leetcode 449


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 {
    string serialize(TreeNode* root) {
        string result;
        return result;
    void getFront(TreeNode* root,string &result){
        if(!root) return;

    TreeNode* deserialize(string data) {
        if(data.length()==0||data.length()==1) return 0;
        TreeNode *result = new TreeNode;
        int i=0;result->val = 0;
            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;
            int tmp = 0;
                tmp=tmp * 10 + data[i]-'0';
            if(tmp>result->val) break;
            else end = i;
            left = data.substr(start,end-start+1);
            TreeNode *nl = deserialize(left);
            if(nl) result->left = nl;
            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;
        for(int i=0;i<data.length();++i){
            int tmp = 0;
            TreeNode *tmpN = que.front();
            if(data[i]==' '){
                if(tmpN->left)  tmpN->left->left = 0x00;
                if(tmpN->right) tmpN->right->right = 0x00;
            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->right = 0x00;tmpN->right = new TreeNode();
        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

Recommended Today

Common auxiliary classes

CountDownLatch Subtraction counter import java.util.concurrent.CountDownLatch; //Counter public class CountDownLatchDemo { public static void main(String[] args) throws InterruptedException { //The total number is 6. It can be used when the task must be performed CountDownLatch countDownLatch = new CountDownLatch(6); for (int i = 0; i < 6; i++) { new Thread(()->{ System.out.println(Thread.currentThread().getName()+” Go out”); countDownLatch.countDown(); },String.valueOf(i)).start(); […]