初音未来 • 1个月前
#include <bits/stdc++.h>
using namespace std;
typedef struct node {
char data;
node *lchild, *rchild;
} tree, *treelink;
void Create_tree(treelink &t, string pre, string in) {
if (pre.size() == 0) {
t = NULL;
return ;
}
char root = pre[0];
int index = in.find(root);
string left_in = in.substr(0, index);
string right_in = in.substr(index + 1);
int len_left = left_in.size();
int len_right = right_in.length();
string left_pre = pre.substr(1, len_left);
string right_pre = pre.substr(len_left + 1);
t = new tree;
t->data = root;
Create_tree(t->lchild, left_pre, left_in);
Create_tree(t->rchild, right_pre, right_in);
}
void last(treelink t) {
if (t != NULL) {
last(t->lchild);
last(t->rchild);
cout << t->data;
}
}
int main() {
string pre, in;
cin >> pre >> in;
treelink t;
Create_tree(t, pre, in);
last(t);
return 0;
}
评论:
请先登录,才能进行评论