刷题三

来源:互联网 发布:挂号网软件 编辑:程序博客网 时间:2024/06/06 00:57

给出一串数字,判断是否为有效二叉查找树的后序遍历序列(及是否能够通过这串后序遍历序列构造出二叉查找树)

#include <iostream>#include <vector>using namespace std;class Solution{    public:    bool myCheck(vector<int> & array){        int length = array.size();        if(length <= 1)            return true;        return recursiveCheck(array,0, length - 1);    }        bool recursiveCheck(vector<int> & array, int begin, int end){        if(begin >= end)            return true;        int i = end - 1;        while(i >= begin && array[i] > array[end])            i --;        int mid = i;        while(i >= begin){            if(array[i] >= array[end])                return false;            i --;        }        return recursiveCheck(array,begin,mid) && recursiveCheck(array,mid + 1,end - 1);    }};int main() {cout  << "test\n";Solution sol;int arrayTemp[] = {5,4,3,8,9,10,6};vector<int> array (arrayTemp, arrayTemp + 7);if(sol.myCheck(array))    cout << "true" << endl;else    cout << "false" << endl;return 0;}