程序员面试100题之六,判断整数序列是不是二元查找树的后序遍历结果

// 100_6.cpp : Defines the entry point for the console application.

//



#include "stdafx.h"



bool verify(int * arr, int len)

{

	if(arr == NULL || len <=0)

		return false;



	int root = arr[len-1];



	int i=0;

	for(;i<len-1;i++)

	{

		if(arr[i]>root)

			break;

	}



	int j=i;

	for(;j<len-1;j++)

	{

		if(arr[j]<root)

			return false;

	}



	bool left = true;

	if(i>0)

		left = verify(arr,i);



	bool right = true;

	if(i<len-1)

		right = verify(arr+i,len-i-1);

	

	return left&&right;

}



int _tmain(int argc, _TCHAR* argv[])

{

	int arr1[] = {5,7,6,9,11,10,8};

	int arr2[] = {7,4,6,5};

	if(verify(arr1,7))

		printf("yes\n");

	else

		printf("no\n");

	if(verify(arr2,4))

		printf("yes\n");

	else

		printf("no\n");

	

	return 0;

}



你可能感兴趣的:(程序员)