Description:

A binary tree is a finite set of vertices that is either empty or
consists of a root r and two disjoint binary trees called the left and
right subtrees. There are three most important ways in which the
vertices of a binary tree can be systematically traversed or ordered.
They are preorder, inorder and postorder. Let T be a binary tree with
root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of
T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in
inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1
in postorder, then the vertices of T2 in postorder and finally we visit
r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.

Input:

The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.

Output:

For each test case print a single line specifying the corresponding postorder sequence.

Sample Input:

9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6

Sample Output:

7 4 2 8 9 5 6 3 1

题目链接

由二叉树的前序遍历的中序遍历求后序遍历。

前序遍历和中序遍历相应区间中前序遍历第一个数一定是当前树的根节点,在中序遍历中找到这个数,区间中这个数左边的数字都在此数左孩子中的小树里,区间中这个数右边的数字都在此数右孩子中的小树里,递归建树、遍历、删除即可。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3+5;
const int mod = 1e9+7;
const double eps = 1e-8;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}

struct tree {
	int data;
	tree *LeftChild, *RightChild;
	tree(): data(INF), LeftChild(NULL), RightChild(NULL) {}
};

int n;
int cnt;
tree *root;
int front[maxn];
int mid[maxn];

void init() {
	cnt = 0;
	root = new tree;
	mem(front, 0);
	mem(mid, 0);
}

int findroot(int lfront, int rfront, int lmid, int rmid) {
	int index;
	for (int i = lmid; i <= rmid; ++i) {
		if (mid[i] == front[lfront]) {
			index = i;
		}
	}
	return index;
}

void addtree(int lfront, int rfront, int lmid, int rmid, tree *r) {
	int index = findroot(lfront, rfront, lmid, rmid);
	r -> data = mid[index];
	if (lmid <= index - 1) {
		r -> LeftChild = new tree;
		addtree(lfront + 1, index - lmid, lmid, index - 1, r -> LeftChild);
	}
	if (index + 1 <= rmid) {
		r -> RightChild = new tree;
		addtree(lfront + 1 + index - lmid, rfront, index + 1, rmid, r -> RightChild);
	}
}

void behind(tree *v) {
	if (v == NULL) {
		return;
	}
	behind(v -> LeftChild);
	behind(v -> RightChild);
	if (cnt++) {
		printf(" ");
	}
	printf("%d", v -> data);
}

void remove(tree *u) {
	if (u == NULL) {
		return;
	}
	remove(u -> LeftChild);
	remove(u -> RightChild);
	delete u;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	while (~scanf("%d", &n)) {
		init();
		for (int i = 1; i <= n; ++i) {
			read(front[i]);
		}
		for (int i = 1; i <= n; ++i) {
			read(mid[i]);
		}
		addtree(1, n, 1, n, root);
		behind(root);
		printf("\n");
		remove(root);
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}