#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <stack>
using namespace std;
//Binary Tree Node
typedef struct BTNode {
char data;
struct BTNode* pLeftChild;
struct BTNode* pRightChild;
} BTNode, * PBTNode;
void visit(PBTNode pT) {
cout << (*pT).data << " ";
}
void preOrder(PBTNode pT) {
if (pT == NULL) {
return ;
}
visit(pT);
preOrder(pT->pLeftChild);
preOrder(pT->pRightChild);
}
void inOrder(PBTNode pT) {
if (pT == NULL) {
return ;
}
inOrder(pT->pLeftChild);
visit(pT);
inOrder(pT->pRightChild);
}
void postOrder(PBTNode pT) {
if (pT == NULL) {
return ;
}
postOrder(pT->pLeftChild);
postOrder(pT->pRightChild);
visit(pT);
}
PBTNode rebulidBinaryTree(string pre, string in, int s1, int e1, int s2, int e2) {
if (s1 > e1) {
return NULL;
}
// pre: 根 左 右
// in: 左 根 右
// post: 左 右 根
PBTNode pT = (PBTNode)malloc(sizeof(BTNode));
pT -> pLeftChild = NULL;
pT -> pRightChild = NULL;
pT -> data = pre[s1];
int mid;
for (int i = s2; i <= e2; i++) {
if (in[i] == pre[s1]) {
mid = i;
break;
}
}
int k1 = mid - s2;
int k2 = e2 - mid;
pT -> pLeftChild = rebulidBinaryTree(pre, in, s1 + 1, s1 + k1, s2, mid - 1);
pT -> pRightChild = rebulidBinaryTree(pre, in, s1 + k1 + 1, e1, mid + 1, e2);
return pT;
}
int main() {
string pre, in, post;
cin >> pre;
cin >> in;
PBTNode pT = rebulidBinaryTree(pre, in, 0, pre.size() - 1, 0, in.size() - 1);
postOrder(pT);
}
#include <iostream>
#include <cstdlib>
using namespace std;
string pre; // 储存前序遍历
string in; // 储存中序遍历
// binary tree node
typedef struct Node {
char data;
struct Node* pLeftChild;
struct Node* pRightChild;
} Node ,* PNode ;
PNode rebulidBinaryTree(string pre,string in,int s1,int e1,int s2,int e2) {
// 写成了 s1 > e2 多捞啊!!
if (s1 > e1) {
return NULL;
}
PNode pT = (PNode)malloc(sizeof(Node));
pT->data = pre[s1];
pT->pLeftChild = pT->pRightChild = NULL;
int mid;
for (int i = s2; i <= e2; i++) {
if (in[i] == pre[s1]) {
mid = i;
break;
}
}
// cout << "mid is: " << mid << ' ' << in[mid] << endl;
int k1 = mid - s2;
int k2 = e2 - mid;
// cout << "k1 k2 is "<< k1 << " " << k2 << endl;
pT->pLeftChild = rebulidBinaryTree(pre, in, s1 + 1, s1 + k1, s2, mid - 1);
pT->pRightChild = rebulidBinaryTree(pre, in, s1 + k1 + 1, e1, mid + 1, e2);
return pT;
}
void postorder(PNode pT) {
if (pT == NULL) {
return ;
}
postorder(pT->pLeftChild);
postorder(pT->pRightChild);
cout << (*pT).data;
}
int main() {
while (cin >> pre >> in) {
PNode pT = rebulidBinaryTree(pre, in, 0, pre.size() - 1, 0, in.size() - 1);
postorder(pT);
cout << endl;
}
}