首页 > 代码库 > 中序遍历 后序遍历 恢复二叉树

中序遍历 后序遍历 恢复二叉树



中序遍历:dbeafc

后序遍历:debfca


#include <stdio.h>#include <stdlib.h>#include <string.h>

void RestoreTree(char *in,char *post,int len ,int treeLen,char* out, int index)
{
	if(index>=treeLen) return;
	out[index] = post[len-1];
	int i = 0;
	for(; i < len; i++)
	{
		if(in[i] == post[len-1])
				  break;
	} 
	
	RestoreTree(in, post, i ,treeLen,out,index*2+1);
	RestoreTree(in+i+1, post + i, len-i-1,treeLen,out,index*2+2);
} 

int main(int argc, char** argv) {
	
	char in[] ="dbeafc"; 
	char post[]="debfca";
	int len = strlen(in);
	char *out = new char[len+1];
	
	memset(out,0,len+1);
	RestoreTree(in,post,len,len,out,0);
	puts(out);
	
	delete []out;
	return 0;
}