bool VerifySquenceOfBST(int sequence[], int length)
{
    if(sequence == NULL || length <= 0)
        return false;

    int root = sequence[length - 1];

    // nodes in left sub-tree are less than root node
    int i = 0;
    for(; i < length - 1; ++ i)
    {
        if(sequence[i] > root)
            break;
    }

    // nodes in right sub-tree are greater than root node
    int j = i;
    for(; j < length - 1; ++ j)
    {
        if(sequence[j] < root)
            return false;
    }

    // Is left sub-tree a binary search tree?
    bool left = true;
    if(i > 0)
        left = VerifySquenceOfBST(sequence, i);

    // Is right sub-tree a binary search tree? 
    bool right = true;
    if(i < length - 1)
        right = VerifySquenceOfBST(sequence + i, length - i - 1);

    return (left && right);
}
    bool VerifySquenceOfBST(vector<int> sequence) 
	{
		if(sequence.empty())
		{
			return true;
		}

		vector<int>::iterator root = sequence.end();
		root--;
		vector<int>::iterator flag1 = sequence.begin() , flag;
		while((*flag1) < (*root))
			flag1++;
		
		flag = flag1;
		
		while(flag1 != root)
		{
			if(*flag1 < *root)
				return false;
			flag1++;
		}
		
		vector<int> left(sequence.begin(), flag);
		vector<int> right(flag, root);
		
		return VerifySquenceOfBST(left) && VerifySquenceOfBST(right);
    }
// ==================== Test Code ====================
void Test(char* testName, int sequence[], int length, bool expected)
{
    if(testName != NULL)
        printf("%s begins: ", testName);

    if(VerifySquenceOfBST(sequence, length) == expected)
        printf("passed.\n");
    else
        printf("failed.\n");
}