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
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<cstdio>
#include<queue>
#include<stack>
#include<set>
#include<vector>
using namespace std;
/*
先按照先序和中序创建树,然后对得到的树进行后序遍历
*/
int pre[1010],in[1010],ans[1010];//先序数组和中序数组,得到的后续数组
int k;//用来输出得到的树所用的下标
struct node{
int value;
node *l,*r;
node(int value = 0,node *l = NULL,node *r = NULL):value(value),l(l),r(r){};//结构体内的有参构造函数和初始化列表 {函数体一般为空}
//无参构造函数初始化格式为node():value(0),l(NULL),r(NULL);
};
//树的创建函数
void build(int l,int r,int &t,node *&root){//&代表传址而不是传值,因为t和指针root要在函数体内变化
//l代表传入的子数组的左下标,r为相应的最右下标
int flag ;
flag = -1;
for(int i = l;i <= r;i++){
if(in[i] == pre[t]){//寻找与先序遍历中根节点字母值相同的字母在中序遍历数组中的下标
flag= i;//记录该位置
break;//找到后断掉执行下面的
}
}
if(flag == -1){//递归出口:如果没找到,flag还是-1,说明没有了直接return
return; //此部需要在root赋值之前,否组in[-1]报错
}
root = new node(in[flag]);//new此处用法相当于malloc,找到了开辟一个指针地址并赋值为in[flag]对应的值
t++;//接着找第二个根节点在in[]中的位置
if(flag > l){//如果flag = i = l;说明中序in[]当前根节点左边的数字已经遍历完,无需再对左边子数组进行左递归
build(l,flag - 1,t,root->l);//传入新的t和root继续递归左边子树
}
if(flag < r){//如果flag = i = r;说明中序in[]当前根节点右边的数字已经遍历完,无需再对右边子数组进行右递归
build(flag + 1,r,t,root->r);//传入新的t和root继续递归右边子树
}
}
void Post(node *root){//二叉树后续遍历算法
if(root != NULL){
Post(root->l);
Post(root->r);
ans[k++] = root->value;
}
}
int main(){
int n = 0;
while(cin >> n){
for(int i = 1; i <= n; i++){
cin >> pre[i];
}
for(int i = 1; i <= n; i++){
cin >> in[i];
}
node *root;//定义一个根节点
int t;//先序下标传参t
t = 1;
k = 0;
build(1,n,t,root);//传入参数创建树
Post(root);
for(int i = 0; i < k -1; i++){
printf("%d ",ans[i]);
}
printf("%d\n",ans[k - 1]);
}
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。