首页 > 代码库 > 剑指offer---二叉搜索树的后序遍历序列

剑指offer---二叉搜索树的后序遍历序列

class Solution
{
public:
    bool VerifySquenceOfBST(vector<int> sequence)
    {
        vector<int> right;
        vector<int> left;

        int size = sequence.size();
        int last = sequence[size - 1];

        if (size== 0) 
        {
            return false;
        }

        //left
        int mid = 0;
        for (int i = 0; i<(size); ++i)
        {
            if (sequence[i] < last)
            {
                left.push_back(sequence[i]);
                ++mid;
            }
            else break;
        }

        //right
        for (int j = mid; j < (size-1); ++j)
        {
            if (sequence[j]>last)
            {
                right.push_back(sequence[j]);
            }
            else
            {
                return false;
                break;
            }
        }

        bool leftresult=true;
        bool rightresult=true;

        if (!left.empty())
        {
            bool leftresult = VerifySquenceOfBST(left);
        }
    
        if (!right.empty())
        {
            bool rightresult = VerifySquenceOfBST(right);
        }

        return(leftresult&&rightresult);


    
    
    }

};

 

剑指offer---二叉搜索树的后序遍历序列