# 二叉树------二叉搜索树的后序遍历序列

＃include <iostream>
using namespace std;

bool VerifyResult(int arr[], int length);

int main()
{
int array_1[] = {5, 7, 6, 9, 11, 10, 8};
int array_2[] = {5, 7, 18, 9, 11, 10, 8};
bool res = false;

res = VerifyResult(array_1, sizeof(array_1)/sizeof(int));
if(res)
cout << "It is a pastOrder of BST" << endl;
else
cout << "Not a pastOrder of BST" << endl;

res = VerifyResult(array_2, sizeof(array_2)/sizeof(int));
if(res)
cout << "It is a pastOrder of BST" << endl;
else
cout << "Not a pastOrder of BST" << endl;

return 0;
}

bool VerifyResult(int arr[], int length)
{
if(arr == NULL || length < 1)
return false;
int root = arr[length - 1];
int i = 0, j = 0;
for(;i < length - 1; i++)   //寻找右子树的开始位置
{
if(arr[i] > root)
break;
}

j = i;
for(;j < length - 1; j++)   //判断右子树中是否有不符合要求的元素
{
if(arr[j] < root)
return false;
}

bool left = true;
if(i > 0)
{
left = VerifyResult(arr, i);
}
bool right = true;
if(i < length - 1)
{
right = VerifyResult(arr, length - i - 1);
}
return (left && right);
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：二叉树------二叉搜索树的后序遍历序列 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)