In 2016, the Group Program was designed for the Tiandao competition-the simulation competition was given the central and post-order traversal to find the pre-order traversal. To solve this type of problem, you can first construct a binary tree and then... It's easy.
Given the post-and Middle-order traversal of a binary tree, please output ITS sequence traversal. Here we assume that the key values are all positive integers that are not equal to each other.
Input Format:
The first line of the input is a positive integer N (<= 30), which is the number of nodes in the binary tree. The second row shows the post-order traversal sequence. The third row shows the sequential traversal sequence. Numbers are separated by spaces.
Output Format:
Output the sequence traversal of the tree in a row. Numbers are separated by one space. No extra space is allowed at the beginning and end of a line.
Input example:
72 3 1 5 7 6 41 2 3 4 5 6 7
Output example:
4 1 6 3 5 7 2
The last node in the Post-order traversal is the root of the tree. Then the left subtree and the right subtree are recursive from the root node. Construct a binary tree. Then BFS goes over and finds the hierarchical sequence.
/* ***********************************************Author :guanjunCreated Time :2016/5/15 19:30:57File Name :1.cpp************************************************ */#include <iostream>#include <cstring>#include <cstdlib>#include <stdio.h>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <iomanip>#include <list>#include <deque>#include <stack>#define ull unsigned long long#define ll long long#define mod 90001#define INF 0x3f3f3f3f#define maxn 10010#define cle(a) memset(a,0,sizeof(a))const ull inf = 1LL << 61;const double eps=1e-5;using namespace std;struct node{ int l,r;}nod[maxn];int in[maxn];int po[maxn];int build(int l1,int r1,int l2,int r2){ if(l1>r1)return 0; int root=po[r2]; int p=l1; while(in[p]!=root)p++; int cnt=p-l1; nod[root].l=build(l1,p-1,l2,l2+cnt-1); nod[root].r=build(p+1,r1,l2+cnt,r2-1); return root;}void bfs(int x){ queue<int>q; q.push(x); vector<int>v; while(!q.empty()){ int w=q.front();q.pop(); if(w==0)break; v.push_back(w); if(nod[w].l!=0)q.push(nod[w].l); if(nod[w].r!=0)q.push(nod[w].r); } int m=v.size(); for(int i=0;i<m;i++){ printf("%d%c",v[i],i==m-1?10:‘ ‘); }}int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int n; while(cin>>n){ for(int i=1;i<=n;i++)cin>>po[i]; for(int i=1;i<=n;i++)cin>>in[i]; build(1,n,1,n); int root=po[n]; bfs(root); } return 0;}
Purple Book examples ..
L2-006. tree traversal