|
输入格式 Input Format | |||
第一行为二叉树的中序序列 第二行为二叉树的后序序列 | |||
输出格式 Output Format | |||
一行,为二叉树的先序序列 |
/////////////
#include <iostream>
using namespace std;
void print(char *midOrder, char *backOrder){
if(strlen(backOrder)==0)
return ;
char c = backOrder[strlen(backOrder)-1];
cout<<c;
unsigned i;
for(i=0; i<strlen(midOrder)-1; i++){
if(midOrder[i]==c)
break;
}
char *rmid = new char[strlen(midOrder)-i];
char *rback = new char[strlen(midOrder)-i];
strcpy(rmid,&midOrder[i+1]);
backOrder[strlen(backOrder)-1] = '\0';
strcpy(rback,&backOrder[i]);
midOrder[i] = '\0';
backOrder[i] = '\0';
print(midOrder,backOrder);
print(rmid,rback);
delete[] rmid;
delete[] rback;
}
int main(){
char midOrder[10],backOrder[10];
cin>>midOrder>>backOrder;
print(midOrder,backOrder);
return 0;
}
评论