The sword refers to the problem-solving ideas and codes of offer18-20 (mirror tree, clockwise printing matrix, stack containing min function)

18 Mirror tree
If you are exposed to this topic for the first time, it is still very difficult, but if you master the method, it is not very difficult. It is mainly necessary to know that for each node, the nodes you save are its subtrees, and the saved Not only nodes, but also their corresponding subordinate relationships. So after it is clear, the solution to this problem is: create a variable to save one of the subtrees, change the order of the two sides, and recursively call the function if there are subtrees until the leaf node is reached. code show as below:
class Solution {
public:
    void Mirror(TreeNode *pRoot) {
        if(pRoot==NULL||(pRoot->left==NULL&&pRoot->right==NULL))
            return;
        TreeNode* pTemp=pRoot->left;
        pRoot->left=pRoot->right;
        pRoot->right=pTemp;
        if(pRoot->right)
            Mirror(pRoot->right);
        if(pRoot->left)
            Mirror(pRoot->left);
    }
};

19 Print the matrix clockwise 
The difficulty of this problem is still there. It mainly needs to consider the judgment conditions of the memory loop. According to the solution in the book, the code is migrated, but there are still many problems. code show as below:
class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        int row=matrix.size();
        int col=matrix[0].size();
        vector<int> result;
        if(matrix.empty())
            return result;
        int start=0;
        while(row>start*2&&col>start*2){
            result=MaxPrint(matrix,row,col,start);
            start++;
        }
        return result;
    }
    vector<int> MaxPrint(vector<vector<int>> matrix,int row,int col,int start){
        int endx=col-1-start;
        int endy= row-1-start;
        vector<int> result;
        for(int i=start;i<=endx;i++){
            int n=matrix[start][i];
            result.push_back(n);
        }
        if(start<endy){
            for(int i=start+1;i<=endy;i++){
                int n=matrix[i][endx];
                result.push_back(n);
            }
         }
            if(start<endx&&start<endy){
                for(int i=endx-1;i>=start;i--){
                    int n=matrix[endy][i];
                    result.push_back(n);
                }
            }
            if(start<endx&&start<endy-1){
                for(int i=endy-1;i>start;i--){
                    int n=matrix[i][start];
                    result.push_back(n);
            }
        }
        return result;
    }
};
The above solution can only output the innermost loop, so you need to define a variable vector<int> answer;
insert the code in the loop for(int i=0;i<result.size();++i)
                                 answer.push_back( result[i]);
The result can be achieved by pushing the result element into the answer continuously. In other words, the functional functions added for the readability of the code do cause some trouble to the actual implementation. If you directly write the function function into the loop body, it will be much more convenient. Attach the correct code:
class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        int row=matrix.size();
        int col=matrix[0].size();
        vector<int> result;
        vector<int> answer;
        if(matrix.empty())
            return result;
        int start=0;
        while(row>start*2&&col>start*2){
            result=MaxPrint(matrix,row,col,start);
            for(int i=0;i<result.size();++i)
                answer.push_back(result[i]);
            start++;
        }
        return answer;
    }
    vector<int> MaxPrint(vector<vector<int>> matrix,int row,int col,int start){
        int endx=col-1-start;
        int endy= row-1-start;
        vector<int> result;
        for(int i=start;i<=endx;i++){
            int n=matrix[start][i];
            result.push_back(n);
        }
        if(start<endy){
            for(int i=start+1;i<=endy;i++){
                int n=matrix[i][endx];
                result.push_back(n);
            }
         }
            if(start<endx&&start<endy){
                for(int i=endx-1;i>=start;i--){
                    int n=matrix[endy][i];
                    result.push_back(n);
                }
            }
            if(start<endx&&start<endy-1){
                for(int i=endy-1;i>start;i--){
                    int n=matrix[i][start];
                    result.push_back(n);
            }
        }
        return result;
    }
};
20 stack containing min function
class Solution {
public:
    void push(int value) {
        stack.push(value);
    }
    void pop() {
        if()
        stack.pop();
    }
    int top() {
        return stack.top();
    }
    int min() {
        stackmin=stack.top();
        stack.pop();
        //while(stack.top()!=NULL){
            if(stackmin>stack.top())
                stackmin=stack.top();
            stack.pop();
        //}
        return stackmin;
    }
private:
    stack<int> stack;
    int stackmin;
};
At first I thought that an int type should be used to solve the problem of storing the smallest element, but I found that the memory was out of bounds. There is no guarantee that the next smallest element will pop out of the stack after the stack. So this problem should be implemented using a minimal stack.
code show as below:
class Solution {
public:
    void push(int value) {
        stk.push (value);
        if(stkmin.empty())
            stkmin.push(value);
        if(stkmin.top()>value)
            stkmin.push(value);
    }
    void pop() {
        if (stkmin.top () == stk.top ())
            stkmin.pop();
        stk.pop ();
    }
    int top() {
        return stk.top();
    }
    int min() {
        return stkmin.top();
    }
private:
    stack <int> stk;
    stack<int> stkmin;
};
It should also be noted that if it is defined as stack like the first piece of code, it cannot be compiled. Although the name is very direct, it may be because there is a function of the same name encapsulated in C++, so try to use other names as member variables.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325848009&siteId=291194637