文章
99
粉丝
120
获赞
8
访问
96.8k
#include<iostream>
using namespace std;
typedef struct node {
char data;
struct node* lchild, * rchild;
}node, * Tree;
void createTree(Tree& T, string pre, string in) {
T = new node;
T->lchild = NULL;T->rchild = NULL;
T->data = pre[0];
if (pre.size() == 1) return;
int mid = in.find(pre[0]);
//没有左子树
if (mid != 0) {
createTree(T->lchild, pre.substr(1, mid + 1), in.substr(0, mid));
}
//没有右子树
if (mid != in.size() - 1) {
createTree(T->rchild, pre.substr(mid + 1), in.substr(mid + 1));
}
}
void postOrder(Tree T) {
if (T != NULL) {
postOrder(T->lchild);
postOrder(T->rchild);
cout << T->data;
}
}
int main() {
string pre, in;
cin >> pre >> in;
Tree T;
createTree(T, pre, in);
postOrder(T);
}
登录后发布评论
暂无评论,来抢沙发