已知前序中序 求后序-c代码实现

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

/*此代码并未考虑边界和错误处理,只体现思路*/
void GetRear(char* pre, char* mid)
{

	char left[10];
    char right[10];
	char pre_left[10];
	char pre_right[10] ;
	char root = pre[0];
	int index = 0;
	int i = 0;

	int midlen = strlen(mid);
	int prelen = strlen(pre);


	if(strlen(mid) == 0)
	{
		return;
	}

	for(index = 0; index < midlen; index ++)
	{
		if(mid[index] == root)
		{
			break;
		}

	}

	for(i = 0; i < index; i ++)
	{
		left[i] = mid[i];
			pre_left[i] = pre[i+1];
	}
	left[i] = '\0';
	pre_left[i] ='\0';



	for(i = index+1; i < midlen; i ++)
	{
		right[i- index-1] = mid[i];
			pre_right[i- index-1] = pre[i];
	}
	    pre_right[i- index-1] ='\0';
		right[i- index-1] = '\0';
 


     	GetRear(pre_left,left);
		GetRear(pre_right,right);

		printf("-%c\r\n",root);
	
	
}

int main()
{
	char a[] = "12473568"; //前序
	char b[] = "47215386"; //中序
	GetRear(a,b);
//	printf("%s",a);

}

你可能感兴趣的:(数据结构)