代码:
//判断是否是某个二叉搜索树的后序遍历序列
bool postOrderTraversalOfBST(int array[], int length)
{
if (array == nullptr || length <= 0)
return false;
int root = array[length - 1];
int i = 0;
for (; i < length - 1; i++)
{
if (array[i] > root)
break;
}
int j = i;
for (; j < length - 1; j++)
{
if (array[j] < root)
return false;
}
//判断左子树是不是二叉搜索树
bool leftTree = true;
if (i > 0)
postOrderTraversalOfBST(array, i );
//判断右子树是不是二叉搜索树
bool rightTree = true;
if (length - i - 1> 0)
postOrderTraversalOfBST(array+i,length - i - 1 );
return (leftTree&rightTree);
}