看了in和out,输入对应的in都是对应的out,但就是一个都没过
查看原帖
看了in和out,输入对应的in都是对应的out,但就是一个都没过
1293644
2320508123hzc楼主2024/4/16 17:45
#include<iostream>
using namespace std;
const int max1 = 17;
struct tree {
	int lc, rc;
	char data;
};
tree tree1[max1];
char z[max1], h[max1], l[max1];
void jian(int hhead,int zhead,int g) //hhead为后序遍历中第一个字母所在位置,zhead为中序遍历中第一个字母所在位置,g为子树的结点数
{
	int j=0;
	for (; j < g; j++) {
		if (h[hhead] == z[zhead+j])break;
	}
	tree1[hhead].data = h[hhead];
	if (g - j - 1 > 0) {
		tree1[hhead].rc = hhead + 1;
	}
	if (j>0) {
		tree1[hhead].lc = hhead + (g - j);
	}
	if (j>=1)	jian(hhead + g - j, zhead, j);//左子树
	if (g - j - 1 >= 1)	jian(hhead + 1, zhead + j + 1, g - j - 1);//右子树
}
void chu(int n) {
	if (tree1[n].data != NULL)cout << tree1[n].data;
	if (tree1[n].lc != NULL) chu(tree1[n].lc);
	if (tree1[n].rc != NULL) chu(tree1[n].rc);
}
int main() {
	char a;
	int n = 0, m;
	for (; (a = getchar()) != '\n'; n++)z[n] = a;
	m = n-1;
	for (; (a = getchar()) != '\n'; m--)h[m] = a;
	jian(0, 0, n);
	chu(0);
	return 0;
}
2024/4/16 17:45
加载中...