程序员wb

玄笙依旧怜  •  1个月前


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;

typedef struct node {
	char data;
	node *lc, *rc;
} tree, *treelink;

void build(treelink &t, string in, string last) {
	if (last.size() == 0) {
		t = NULL;
		return;
	}
	char root = last[last.size() - 1];
	int index = in.find(root);
	string left_in = in.substr(0, index);
	string right_in = in.substr(index + 1);
	int left_len = left_in.size();
	int right_len = right_in.size();
	string left_last = last.substr(0, left_len);
	string right_last = last.substr(left_len, right_len);
	t = new tree;
	t->data = root;
	build(t->lc, left_in, left_last);
	build(t->rc, right_in, right_last);
}

void pre(treelink t) {
	if (t != NULL) {
		cout << t->data;
		pre(t->lc);
		pre(t->rc);
	}
}

int main() {//0shi48
	//freopen("wenjian.in","r",stdin);
	//freopen("wenjian.out","w",stdout);
	string in, last;
	treelink t;
	cin >> in >> last;
	build(t, in, last);
	pre(t);
	return 0;
}

评论:

请先登录,才能进行评论