样例输入:CEFDBHGA
CBEDFAGH
我的输出:AGHBDFEC
样例输出:ABCDEFGH
typedef struct btree {
char data;
struct btree* left;
struct btree* right;
}bt,*btnode;
btnode creat(int po1, int po2, int io1, int io2, string& str1, string& str2) {//分别表示后根序列和中根序列首尾元素的坐标值
if (po1 >po2)
return NULL;
btnode root = new bt;
root->left = NULL;
root->right = NULL;
root->data = str1[po2];
int i;
for ( i = io1; i <= io2; i++) {
if (str1[i] == root->data) {
break;
}
}
root->left = creat(po1, po1 + i - 1, io1, io1 + i - 1, str1, str2);
root->right = creat(po1 + i, po2 - 1, io1 + i + 1, io2, str1, str2);
return root;
}
void pretree(btnode t) {
if (!t)
return ;
cout << t->data;
pretree(t->left);
pretree(t->right);
}
int main() {
int io1=0, io2=0, po1=0, po2=0;
string sIn, sPost;
getline(cin, sPost);
getline(cin, sIn);
io2 = sIn.length() - 1;
po2 = sPost.length() - 1;
btnode root = creat(po1, po2, io1, io2, sPost, sIn);
pretree(root);
return 0;
}
CEFDBHGA
CBEDFAGH
AGHBDFEC
我的解答思路和尝试过的方法
我想要达到的结果