The sword refers to offer21-23 problem-solving ideas and codes (stack push, pop sequence, top-to-bottom printing of binary tree, post-order traversal sequence of binary search tree)

21 stack push and pop sequence
This problem has a problem-solving idea, which is to simulate the pop-up sequence. First, insert elements into a stack continuously, and then compare the output stack to start popping elements. If the value in the popped container is different from the value in the popV stack, it means that the pop-up sequence is not.
class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        if(pushV.size()!=popV.size()&&pushV.empty()&&popV.empty())
            return false;
        stack<int> st;
        vector<int> answer;
        for(int i=0,j=0;j<popV.size();){
            while(st.empty()||st.top()!=popV[j]){
                st.push(pushV[i]);
                ++i;
            }
            if(st.top()==popV[j]){
                answer.push_back(st.top());
                st.pop();
                ++j;
            }
        }
        for(int i=0;i<answer.size();i++){
            if(answer[i]!=popV[i])
                return false;
        }
        return true;
    }
};
I compiled this first, and then found that it can only pass 33% of the examples. The main reason is that it can only pass the right examples. When it needs to return false, because the judgment condition is if(st.top()==popV[ j]) so when it's not right j will never increase and get stuck in an infinite loop. . . So the correct judgment is as follows:
class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        if(pushV.size()!=popV.size()&&pushV.empty()&&popV.empty())
            return false;
        stack<int> st;
        vector<int> answer;
        int j=0;
        for(int i=0;j<popV.size();){
            while(st.empty()||st.top()!=popV[j]){
                st.push(pushV[i]);
                ++i;
            }
            if(st.top()!=popV[j])
                break;
                st.pop();
                ++j;
        }
        if(st.empty()&&j==popV.size())
            return true;
        return false;
    }
};

22 Printing a binary tree from top to bottom
The purpose of inputting each node into the answer container is achieved by storing the nodes in the queue. The original idea was to traverse the method, but the queue feels better from the result to the process. code show as below:
class Solution {
public:
    vector<int> PrintFromTopToBottom(TreeNode* root) {
        vector<int> answer;
        if(!root)
            return answer;
        deque<TreeNode*> that;
        que.push_back(root);
        while(!que.empty()){
            TreeNode* rot=que.front();
            que.pop_front();
            answer.push_back(rot->val);
            if(rot->left)
                que.push_back(rot->left);
            if(rot->right)
                que.push_back(rot->right);
        }
        return answer;
    }
};

23 Post-order traversal sequence of binary search tree
This question first needs to know that in post-order traversal, the value of the root node is the last, and in a binary search tree, the value of its left subtree must be less than the root value, and the right subtree must be greater than the root value. Therefore, if the value of the right subtree is less than the root value, the solution is not obtained for post-order traversal. First, the left subtree and the right subtree are obtained by traversing, and then the recursive judgment is made. code show as below:
class Solution {
public:
    bool VerifySquenceOfBST(vector<int> sequence) {
        int length=sequence.size()-1;
        if(sequence.empty())
            return false;
        int i=0;
        int root=sequence[length];
        vector<int> left;
        for(;sequence[i]<root;++i){
            left.push_back(sequence[i]);
        }
        int j=i;
        vector<int> right;
        for(;j<length;++j){
            if(sequence[j]<root)
                return false;
            right.push_back(sequence[j]);
        }
        bool left1=true;
        if(i>0)
            left1=VerifySquenceOfBST(left);
        bool right1=true;
        if(i<length)
            right1=VerifySquenceOfBST(right);
        return left1&&right1;
    }
};

Guess you like

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