5.4 给定前序遍历和中序遍历,返回后序遍历

#include <stdio.h>
#include <string.h>
 
#define MAX 64

// 给定前序遍历和中序遍历,返回后序遍历
void build_pos(const char * pre, const char * in, const int n, char * post){
	int left_len = strchr(in, pre[0]) - in;//strchr不是strchar!

	if(n <= 0) return;

	build_pos(pre+1, in, left_len, post);//左子树
	build_pos(pre+left_len+1, in+left_len+1, n-left_len-1, post+left_len);//右子树,注意最后一个参数是post的首部

	post[n-1] = pre[0];

}

 

int main(){
	char pre[MAX] = {0};
	char in[MAX] = {0};
	char post[MAX] = {0};

	strcpy(pre,"DBACEGF");
	strcpy(in, "ABCDEFG");

	int n = strlen(pre);

	build_pos(pre, in, n, post);

	printf("%s\n", post);


	return 0;
}


你可能感兴趣的:(5.4 给定前序遍历和中序遍历,返回后序遍历)