The sword refers to Offer_Programming questions_23

Topic description

Input an integer array to determine whether the array is the result of a post-order traversal of a binary search tree. If yes, output Yes, otherwise output No. Suppose any two numbers of the input array are different from each other.
class Solution {
public:
    bool VerifySquenceOfBST(vector<int> sequence) {
        if(sequence.size() == 0){
            return false;
        }
        int start = 0;
        int end = sequence.size()-1;
        bool flag = getResult(sequence, start, end);
        return flag;
    }
    bool getResult(vector<int> vt, int start, int end){
        int i,j;
        if(end - start<=1){
            return true;
        }
        for(i = start; i < end; i++){
            if(vt[i]>vt[end]){
                break;
            }
        }
        for(j = i; j < end; j++){
            if(vt[j]<vt[end]){
                return false;
            }
        }
        return getResult(vt,start,i-1)&&getResult(vt,i,end-1);
    }
};

  

Guess you like

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