判断整数序列是不是二叉排序树的后序遍历结果

/*
  1 2 3 6 7 5 4
  
  判断是不是一个合法的二叉排序树后序序列
   
*/
#include<iostream>
using namespace std;

bool  isSeq(int a[],int start , int end)
{
	int position;
	if(start==end)
		return true;
	if(start>end)
		return false;
	
	int root=a[end-1];
	
	int i=start;
	while(a[i]<root && i<end)
	{
		i++;
	}	
	position=i;
	
	while(i++ && i<end)
	{
		if(a[i] < root )
			return false;
	}
	
	bool left=isSeq(a,start,position);
	bool right=isSeq(a,position,end-1);
	
	return (left && right);
	
} 

int main()
{
	int a[]={1,2,3,6,7,5,4};
	
	cout<<isSeq(a,0,7)<<endl;
	
	system("pause");
	
	return 0;
	
}
原文地址:https://www.cnblogs.com/zhanglanyun/p/2662538.html