Given the partial results of a binary tree's traversals in in-order, pre-order, and post-order. You are supposed to output the complete results and the level order traversal sequence of the corresponding tree.
Input Specification:
Each input file contains one test case. For each case, a positive integer N (<=100) is given in the first line. Then three lines follow, containing the incomplete in-order, pre-order and post-order traversal sequences, respectively. It is assumed that the tree nodes are numbered from 1 to N and no number is given out of the range. A "-" represents a missing number.
Output Specification:
For each case, print in four lines the complete in-order, pre-order and post-order traversal sequences, together with the level order traversal sequence of the corresponding tree. The numbers must be separated by a space, and there must be no extra space at the end of each line. If it is impossible to reconstruct the unique tree from the given information, simply print "Impossible".
Sample Input 1:
9
3 - 2 1 7 9 - 4 6
9 - 5 3 2 1 - 6 4
3 1 - - 7 - 6 8 -
Sample Output 1:
3 5 2 1 7 9 8 4 6
9 7 5 3 2 1 8 6 4
3 1 2 5 7 4 6 8 9
9 7 8 5 6 3 2 4 1
Sample Input 2:
3
- - -
- 1 -
1 - -
Sample Output 2:
Impossible
n个结点
中序残缺列
先序残缺列
后序残缺列
求能不能唯一确定一棵二叉树,可以输出中序遍历、先序遍历、后序遍历、层次遍历;每个遍历后面没有多余的空格。
不能唯一确定输出“Impossible”
我一共尝试了5种方法,
一、一种方法是错误的(5个测试点错误);
二、另一种方法考虑没有周全(两个测试点错误);
三、一种方法直接递归(最后一个测试点运行超时)
四。一种方法把递归的拆成两部分,存入多维数组里面(最后一个测试点内存超限)【这个代码中有vector的[]传参】
五、把第三种和第二种部分相结合(AC)
除了第一个,其他代码均如下五、四、三、二
评测结果
测试点
|
0 | 答案正确 | 1 | 304 | 17/17 |
1 | 答案正确 | 1 | 256 | 1/1 |
2 | 答案正确 | 1 | 308 | 9/9 |
3 | 答案正确 | 1 | 308 | 1/1 |
4 | 答案正确 | 1 | 180 | 1/1 |
5 | 答案正确 | 3 | 308 | 2/2 |
6 | 答案正确 | 1 | 180 | 2/2 |
7 | 答案正确 | 1 | 180 | 2/2 |
#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#define NOExist 0
void readln(vector<int>*line, int end_)
{
int index, temp;
string str;
for (index = 1; index< end_; index++)
{
cin >> str;
if (str[0] != '-')
{
sscanf(str.c_str(), "%d", &temp);
(*line)[index] = temp;
}
}
}
int findmidindex(vector<int>*in_order, int goal, int sindex, int eindex)
{
while (sindex <= eindex && (*in_order)[sindex] != goal)sindex++;
return sindex;
}
bool _in_pre_post(vector<int>*post,vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, int sindex, int eindex,
int prs, int pre, int pos, int poe,bool mark)
{
int leftCnt, rightCnt, midindex;
bool flag=true;
if(mark)
{
if (NOExist != (*pre_order)[prs] && NOExist == (*post_order)[poe])
(*post_order)[poe]= (*pre_order)[prs];
else if (NOExist != (*post_order)[poe] && NOExist == (*pre_order)[prs])
(*pre_order)[prs]= (*post_order)[poe] ;
else if (NOExist == (*pre_order)[prs] && NOExist == (*post_order)[poe])
return false;
}
midindex = findmidindex(in_order, (*pre_order)[prs], sindex, eindex);
if(midindex>eindex||!mark&&NOExist != (*post)[poe] && (*post)[poe] != (*pre_order)[prs])return false;
leftCnt = midindex - sindex;
rightCnt = eindex - midindex;
if(!mark) (*post_order)[poe] = (*pre_order)[prs];
if (rightCnt>0)
flag=flag&& _in_pre_post(post,in_order, pre_order, post_order, midindex + 1, eindex, prs + leftCnt + 1, pre, pos + leftCnt, poe - 1,mark);
if (leftCnt >0)
flag=flag&& _in_pre_post(post,in_order, pre_order, post_order, sindex, midindex - 1, prs + 1, prs + leftCnt, pos, pos + leftCnt - 1,mark);
return flag;
}
bool onlychoicePre(vector<int>*post, vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, vector<int>*ti, vector<int>*tpr, vector<int>*tpo,
int prs,int N,int*tag)
{
int mid, midindex;
for (midindex = prs; midindex <= N&&NOExist != (*pre_order)[midindex]; midindex++);
if (midindex <= N)
{
vector<int>tin, tpre, tpos;
int temp;
vector<bool>may(N + 1, true);
for (temp = 1; temp <= N; temp++)
may[(*pre_order)[temp]] = false;
for (mid = 1; mid <= N; mid++)
{
if (may[mid])
{
tin = (*in_order);
tpre = (*pre_order);
tpos = (*post_order);
tpre[midindex] = mid;
if (!onlychoicePre(post, &tin, &tpre, &tpos, ti, tpr, tpo, midindex , N, tag))return false;
}
}
}
else
{
if(_in_pre_post(post,in_order, pre_order, post_order, 1, N, 1, N, 1, N,false))
{
(*tag)++;
if (1 == (*tag))
{
(*ti) = (*in_order);
(*tpr) = (*pre_order);
(*tpo) = (*post_order);
}
else if((*tag) > 1)return false;
}
}
return true;
}
bool onlychoiceIn(vector<int>*post,vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, vector<int>*ti, vector<int>*tpr, vector<int>*tpo,
int sindex, int N,int*tag){
int mid, midindex;
for (midindex = sindex; midindex <= N&&NOExist != (*in_order)[midindex]; midindex++);
if (midindex <=N)
{
vector<int>tin, tpre, tpos;
int temp;
vector<bool>may(N + 1, true);
for (temp = 1; temp <= N; temp++)
may[(*in_order)[temp]] = false;
for (mid = 1; mid <= N; mid++)
{
if (may[mid])
{
tin = (*in_order);
tpre = (*pre_order);
tpos = (*post_order);
tin[midindex] = mid;
if (!onlychoiceIn(post, &tin, &tpre, &tpos, ti, tpr, tpo, midindex, N, tag))return false;
}
}
}
else
{
return onlychoicePre(post, in_order, pre_order, post_order, ti, tpr, tpo, 1, N, tag);
}
return true;
}
void Display(vector<int>*line, int end_)
{
int index;
for (index = 1; index< end_; index++)
{
cout << (*line)[index] << " ";
}
cout << (*line)[index] << endl;
}
struct levelout
{
int prs;
int pre;
int si;
int se;
levelout(int a, int b, int c, int e) :prs(a), pre(b), si(c), se(e){}
};
void leveldisplay(vector<int>*in_order, vector<int>*pre_order, int N)
{
int size, index;
queue<levelout>q;
cout << (*pre_order)[1];
q.push(levelout(1, N, 1, N));
while (!q.empty())
{
size = q.size();
while (size--)
{
index = findmidindex(in_order, (*pre_order)[q.front().prs], q.front().si, q.front().se);
if (index <= q.front().se)
{
if (index - q.front().si> 0)
{
cout << " " << (*pre_order)[q.front().prs + 1];
q.push(levelout(q.front().prs + 1, q.front().prs + index - q.front().si, q.front().si, index - 1));
}
if (q.front().se - index > 0)
{
cout << " " << (*pre_order)[q.front().prs + index - q.front().si + 1];
q.push(levelout(q.front().prs + index - q.front().si + 1, q.front().pre, index + 1, q.front().se));
}
}
q.pop();
}
}
cout << endl;
}
int main()
{
int N,tag;
cin >> N;
vector<int>in_order(N + 1, NOExist), pre_order(N + 1, NOExist), post_order(N + 1, NOExist);
readln(&in_order, N + 1);
readln(&pre_order, N + 1);
readln(&post_order, N + 1);
if(_in_pre_post(&post_order,&in_order, &pre_order, &post_order, 1, N,1, N,1, N,true))
{
Display(&in_order, N);
Display(&pre_order, N);
Display(&post_order, N);
leveldisplay(&in_order, &pre_order, N);
return 0;
}
vector<int> ti, tpr, tpo,post;
ti = in_order;
tpr = pre_order;
post=tpo = post_order;
tag = 0;
if (N>0&&onlychoiceIn(&post, &in_order, &pre_order, &post_order, & ti, &tpr, &tpo, 1, N, &tag)&&1==tag)
{
Display(&ti, N);
Display(&tpr, N);
Display(&tpo, N);
leveldisplay(&ti, &tpr, N);
}
else cout << "Impossible" << endl;
system("pause");
return 0;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 180 | 17/17 |
1 | 答案正确 | 1 | 304 | 1/1 |
2 | 答案正确 | 1 | 316 | 9/9 |
3 | 答案正确 | 1 | 308 | 1/1 |
4 | 答案正确 | 1 | 308 | 1/1 |
5 | 答案正确 | 3 | 712 | 2/2 |
6 | 答案正确 | 1 | 180 | 2/2 |
7 | 内存超限 | | | 0/2 |
#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#define NOExist 0
void readln(vector<int>*line, int end_)
{
int index, temp;
string str;
for (index = 1; index< end_; index++)
{
cin >> str;
if (str[0] != '-')
{
sscanf(str.c_str(), "%d", &temp);
(*line)[index] = temp;
}
}
}
int findmidindex(vector<int>*in_order, int goal, int sindex, int eindex)
{
while (sindex <= eindex && (*in_order)[sindex] != goal)sindex++;
return sindex;
}
bool _in_pre_post(vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, int sindex, int eindex,
int prs, int pre, int pos, int poe)
{
int leftCnt, rightCnt, midindex;
bool flag=true;
midindex = findmidindex(in_order, (*pre_order)[prs], sindex, eindex);
if(midindex>eindex)return false;
leftCnt = midindex - sindex;
rightCnt = eindex - midindex;
(*post_order)[poe] = (*pre_order)[prs];
if (rightCnt>0)
flag=flag&& _in_pre_post(in_order, pre_order, post_order, midindex + 1, eindex, prs + leftCnt + 1, pre, pos + leftCnt, poe - 1);
if (leftCnt >0) flag=flag&& _in_pre_post(in_order, pre_order, post_order, sindex, midindex - 1, prs + 1, prs + leftCnt, pos, pos + leftCnt - 1);
return flag;
}
bool mayright(vector<int>*post, vector<int>*post_order, int pos, int poe)
{
for(;pos<= poe;pos++)
if (NOExist != (*post)[pos] && (*post)[pos] != (*post_order)[pos])
return false;
return true;
}
void Display(vector<int>*line, int end_);
bool onlychoiceIn(vector<vector<int> > *arrayALL,vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order,int N)
{
int inCnt,preCnt,tag=0;
vector<int>ti,tpr,tpo(N+1),post;
post=(*post_order);
inCnt=arrayALL[0].size();
while(inCnt--)
{
preCnt=arrayALL[1].size();
while(preCnt--)
{ ti = arrayALL[0][inCnt];
tpr =arrayALL[1][preCnt];
if(_in_pre_post(&ti, &tpr, &tpo, 1, N, 1, N, 1, N))
{
if (mayright(&post,&tpo, 1, N))
{
tag++;
if (1 == tag)
{
(*in_order)= ti;
(*pre_order)=tpr;
(*post_order)= tpo;
}
}
else if (tag > 1)return false;
}
}
}
return true;
}
void everyWays(vector<vector<int> >*arrayALL,vector<int>*this_order, int sindex, int N){
int emptyRoom;
for (emptyRoom = sindex; emptyRoom <= N&&NOExist != (*this_order)[emptyRoom]; emptyRoom++);
if (emptyRoom <=N)
{
int mid;
vector<int>tin;
vector<bool>may(N + 1, true);
for (mid = 1; mid <= N; mid++)
may[(*this_order)[mid]] = false;
for (mid = 1; mid <= N; mid++)
{
if (may[mid])
{
tin = (*this_order);
tin[emptyRoom] = mid;
everyWays(arrayALL,&tin,emptyRoom, N);
}
}
}else (*arrayALL).push_back((*this_order));
}
void Display(vector<int>*line, int end_)
{
int index;
for (index = 1; index< end_; index++)
{
cout << (*line)[index] << " ";
}
cout << (*line)[index] << endl;
}
struct levelout
{
int prs;
int pre;
int si;
int se;
levelout(int a, int b, int c, int e) :prs(a), pre(b), si(c), se(e){}
};
void leveldisplay(vector<int>*in_order, vector<int>*pre_order, int N)
{
int size, index;
queue<levelout>q;
cout << (*pre_order)[1];
q.push(levelout(1, N, 1, N));
while (!q.empty())
{
size = q.size();
while (size--)
{
index = findmidindex(in_order, (*pre_order)[q.front().prs], q.front().si, q.front().se);
if (index <= q.front().se)
{
if (index - q.front().si> 0)
{
cout << " " << (*pre_order)[q.front().prs + 1];
q.push(levelout(q.front().prs + 1, q.front().prs + index - q.front().si, q.front().si, index - 1));
}
if (q.front().se - index > 0)
{
cout << " " << (*pre_order)[q.front().prs + index - q.front().si + 1];
q.push(levelout(q.front().prs + index - q.front().si + 1, q.front().pre, index + 1, q.front().se));
}
}
q.pop();
}
}
cout << endl;
}
int main()
{
int N;
cin >> N;
vector<int>in_order(N + 1, NOExist), pre_order(N + 1, NOExist), post_order(N + 1, NOExist);
vector<vector<int> >array[2];
readln(&in_order, N + 1);
readln(&pre_order, N + 1);
readln(&post_order, N + 1);
everyWays(&array[0],&in_order,1, N);
everyWays(&array[1],&pre_order,1, N);
if (N>0&&onlychoiceIn(array,&in_order, &pre_order, &post_order,N))
{
Display(&in_order, N);
Display(&pre_order, N);
Display(&post_order, N);
leveldisplay(&in_order, &pre_order, N);
}
else cout << "Impossible" << endl;
system("pause");
return 0;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 384 | 17/17 |
1 | 答案正确 | 1 | 308 | 1/1 |
2 | 答案正确 | 2 | 308 | 9/9 |
3 | 答案正确 | 1 | 384 | 1/1 |
4 | 答案正确 | 1 | 308 | 1/1 |
5 | 答案正确 | 3 | 300 | 2/2 |
6 | 答案正确 | 1 | 304 | 2/2 |
7 | 运行超时 | | | 0/2 |
#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#define NOExist 0
struct trees
{
int _index[2];
};
void readln(vector<int>*line, int end_)
{
int index, temp;
string str;
for (index = 1; index< end_; index++)
{
cin >> str;
if (str[0] != '-')
{
sscanf(str.c_str(), "%d", &temp);
(*line)[index] = temp;
}
}
}
int findmidindex(vector<int>*in_order, int goal, int sindex, int eindex)
{
while (sindex <= eindex && (*in_order)[sindex] != goal)sindex++;
return sindex;
}
bool _in_pre_post(vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, int sindex, int eindex,
int prs, int pre, int pos, int poe)
{
int leftCnt, rightCnt, midindex;
bool flag=true;
midindex = findmidindex(in_order, (*pre_order)[prs], sindex, eindex);
if(midindex>eindex)return false;
leftCnt = midindex - sindex;
rightCnt = eindex - midindex;
(*post_order)[poe] = (*pre_order)[prs];
if (rightCnt>0)
flag=flag&& _in_pre_post(in_order, pre_order, post_order, midindex + 1, eindex, prs + leftCnt + 1, pre, pos + leftCnt, poe - 1);
if (leftCnt >0)
flag=flag&& _in_pre_post(in_order, pre_order, post_order, sindex, midindex - 1, prs + 1, prs + leftCnt, pos, pos + leftCnt - 1);
return flag;
}
bool mayright(vector<int>*post, vector<int>*post_order, int pos, int poe)
{
for(;pos<= poe;pos++)
if (NOExist != (*post)[pos] && (*post)[pos] != (*post_order)[pos])
return false;
return true;
}void Display(vector<int>*line, int end_);
bool onlychoicePre(vector<int>*post, vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, vector<int>*ti, vector<int>*tpr, vector<int>*tpo,
int prs,int N,int*tag)
{
int mid, midindex;
for (midindex = prs; midindex <= N&&NOExist != (*pre_order)[midindex]; midindex++);
if (midindex <= N)
{
vector<int>tin, tpre, tpos;
int temp;
vector<bool>may(N + 1, true);
for (temp = 1; temp <= N; temp++)
may[(*pre_order)[temp]] = false;
for (mid = 1; mid <= N; mid++)
{
if (may[mid])
{
tin = (*in_order);
tpre = (*pre_order);
tpos = (*post_order);
tpre[midindex] = mid;
if (!onlychoicePre(post, &tin, &tpre, &tpos, ti, tpr, tpo, midindex , N, tag))return false;
}
}
}
else
{
if(_in_pre_post(in_order, pre_order, post_order, 1, N, 1, N, 1, N))
{
if (mayright(post, post_order, 1, N))
{
(*tag)++;
if (1 == (*tag))
{
(*ti) = (*in_order);
(*tpr) = (*pre_order);
(*tpo) = (*post_order);
}
}
else if ((*tag) > 1)return false;}
}
return true;
}
bool onlychoiceIn(vector<int>*post,vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, vector<int>*ti, vector<int>*tpr, vector<int>*tpo,
int sindex, int N,int*tag){
int mid, midindex;
for (midindex = sindex; midindex <= N&&NOExist != (*in_order)[midindex]; midindex++);
if (midindex <=N)
{
vector<int>tin, tpre, tpos;
int temp;
vector<bool>may(N + 1, true);
for (temp = 1; temp <= N; temp++)
may[(*in_order)[temp]] = false;
for (mid = 1; mid <= N; mid++)
{
if (may[mid])
{
tin = (*in_order);
tpre = (*pre_order);
tpos = (*post_order);
tin[midindex] = mid;
if (!onlychoiceIn(post, &tin, &tpre, &tpos, ti, tpr, tpo, midindex, N, tag))return false;
}
}
}
else
{
if (!onlychoicePre(post, in_order, pre_order, post_order, ti, tpr, tpo, 1, N, tag))return false;
}
return true;
}
void Display(vector<int>*line, int end_)
{
int index;
for (index = 1; index< end_; index++)
{
cout << (*line)[index] << " ";
}
cout << (*line)[index] << endl;
}
struct levelout
{
int prs;
int pre;
int si;
int se;
levelout(int a, int b, int c, int e) :prs(a), pre(b), si(c), se(e){}
};
void leveldisplay(vector<int>*in_order, vector<int>*pre_order, int N)
{
int size, index;
queue<levelout>q;
cout << (*pre_order)[1];
q.push(levelout(1, N, 1, N));
while (!q.empty())
{
size = q.size();
while (size--)
{
index = findmidindex(in_order, (*pre_order)[q.front().prs], q.front().si, q.front().se);
if (index <= q.front().se)
{
if (index - q.front().si> 0)
{
cout << " " << (*pre_order)[q.front().prs + 1];
q.push(levelout(q.front().prs + 1, q.front().prs + index - q.front().si, q.front().si, index - 1));
}
if (q.front().se - index > 0)
{
cout << " " << (*pre_order)[q.front().prs + index - q.front().si + 1];
q.push(levelout(q.front().prs + index - q.front().si + 1, q.front().pre, index + 1, q.front().se));
}
}
q.pop();
}
}
cout << endl;
}
int main()
{
int N,tag;
cin >> N;
vector<int>in_order(N + 1, NOExist), pre_order(N + 1, NOExist), post_order(N + 1, NOExist), ti, tpr, tpo,post;
readln(&in_order, N + 1);
readln(&pre_order, N + 1);
readln(&post_order, N + 1);
ti = in_order;
tpr = pre_order;
post=tpo = post_order;
tag = 0;
if (onlychoiceIn(&post, &in_order, &pre_order, &post_order, & ti, &tpr, &tpo, 1, N, &tag))
{
Display(&ti, N);
Display(&tpr, N);
Display(&tpo, N);
leveldisplay(&ti, &tpr, N);
}
else cout << "Impossible" << endl;
system("pause");
return 0;
}
评测结果
测试点
|
0 | 答案正确 | 1 | 300 | 17/17 |
1 | 答案正确 | 1 | 252 | 1/1 |
2 | 答案错误 | 1 | 252 | 0/9 |
3 | 答案正确 | 1 | 300 | 1/1 |
4 | 答案正确 | 1 | 300 | 1/1 |
5 | 答案错误 | 1 | 180 | 0/2 |
6 | 答案正确 | 1 | 248 | 2/2 |
7 | 答案正确 | 1 | 308 | 2/2 |
#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#define NOExist 0
void readln(vector<int>*line, int end_)
{
int index, temp;
string str;
for (index = 1; index< end_; index++)
{
cin >> str;
if (str[0] != '-')
{
sscanf(str.c_str(), "%d", &temp);
(*line)[index] = temp;
}
}
}
int findmidindex(vector<int>*in_order, int goal, int sindex, int eindex)
{
while (sindex <= eindex && (*in_order)[sindex] != goal)sindex++;
return sindex;
}
int _in_pre_post(vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, int sindex, int eindex,
int prs, int pre, int pos, int poe, int N);
int onlychoice(vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, int sindex, int eindex,
int prs, int pre, int pos, int poe, int N){
int mid, midindex,tt;
int only = 0;
vector<int>tin, tpre, tpos,ti,tpr,tpo;
if (NOExist != (*post_order)[poe])
{
for (midindex = sindex; midindex <= eindex; midindex++)
{
if (NOExist == (*in_order)[midindex])
{
tin = (*in_order);
tpre = (*pre_order);
tpos = (*post_order);
tin[midindex] = tpos[poe];
tt=_in_pre_post(&tin, &tpre, &tpos, sindex, eindex, prs, pre, pos, poe, N);
if(tt>1)
return 2;
else if(1==tt)
only++;
if(only>1)return 2;
else if (1 == only)
{
ti= tin;
tpr = tpre;
tpo = tpos;
}
}
}
}
else
{
int temp;
vector<bool>may(N +1, true);
queue<int>NOExistq,qtemp;
for (midindex = sindex; midindex <= eindex; midindex++)
{
if (NOExist == (*in_order)[midindex])
NOExistq.push(midindex);
}
for (temp = 1; temp <= N; temp++)
{
if (temp<sindex || temp>eindex)
may[(*in_order)[temp]] = false;
may[(*pre_order)[temp]] = false;
may[(*post_order)[temp]] = false;
}
for (mid = 1; mid <= N; mid++)
{
if (may[mid])
{
midindex = findmidindex(in_order, mid, sindex, eindex);
if (midindex <= eindex)
{
tin = (*in_order);
tpre = (*pre_order);
tpos = (*post_order);
tpre[prs]=tpos[poe]=tin[midindex] = mid;
tt=_in_pre_post(&tin, &tpre, &tpos, sindex, eindex, prs, pre, pos, poe, N);
if(tt>1)
return 2;
else if(1==tt)
only++;
if(only>1)return 2;
else if (1 == only)
{
ti= tin;
tpr = tpre;
tpo = tpos;
}
}
else
{
qtemp = NOExistq;
while(!qtemp.empty())
{
tin = (*in_order);
tpre = (*pre_order);
tpos = (*post_order);
tpre[prs]=tpos[poe]=tin[qtemp.front()] = mid;
tt=_in_pre_post(&tin, &tpre, &tpos, sindex, eindex, prs, pre, pos, poe, N);
if(tt>1)
return 2;
else if(1==tt)
only++;
if(only>1)return 2;
else if (1 == only)
{
ti= tin;
tpr = tpre;
tpo = tpos;
}
qtemp.pop();
}
}
}
}
}
if (1 == only)
{
(*in_order)=ti;
(*pre_order)=tpr;
(*post_order)=tpo;
}
return only;
}
int _in_pre_post(vector<int>*in_order, vector<int>*pre_order, vector<int>*post_order, int sindex, int eindex,
int prs, int pre, int pos, int poe, int N)
{
int mid, leftCnt, rightCnt, midindex,temp;
if (NOExist != (*pre_order)[prs] && NOExist == (*post_order)[poe])
mid = (*post_order)[poe]= (*pre_order)[prs] ;
else if (NOExist != (*post_order)[poe] && NOExist == (*pre_order)[prs])
mid= (*pre_order)[prs]= (*post_order)[poe] ;
else if ((*post_order)[poe] != (*pre_order)[prs])
return 0;
else mid=(*pre_order)[prs];
midindex = findmidindex(in_order, mid, sindex, eindex);
if (NOExist != mid&&midindex <= eindex&&sindex<eindex)
{
leftCnt = midindex - sindex;
rightCnt = eindex - midindex;
if (leftCnt >0)
{
temp=_in_pre_post(in_order, pre_order, post_order, sindex, midindex - 1, prs + 1, prs + leftCnt, pos, pos + leftCnt - 1, N);
if(1!=temp)
return temp;
}
if (rightCnt>0)
{
temp=_in_pre_post(in_order, pre_order, post_order, midindex + 1, eindex, prs + leftCnt + 1, pre, pos + leftCnt, poe - 1, N);
if(1!=temp)
return temp;
}
}
else if (sindex <eindex||sindex==eindex&&NOExist==mid)
{
return onlychoice(in_order, pre_order, post_order, sindex, eindex, prs, pre, pos, poe, N);
}else if(sindex==eindex&&NOExist==(*in_order)[sindex])
(*in_order)[sindex]=mid;
else if(sindex==eindex&&mid!=(*in_order)[sindex])
return 0;
return 1;
}
void Display(vector<int>*line, int end_)
{
int index;
for (index = 1; index< end_; index++)
{
cout << (*line)[index] << " ";
}
cout << (*line)[index] << endl;
}
struct levelout
{
int prs;
int pre;
int si;
int se;
levelout(int a, int b, int c, int e) :prs(a), pre(b), si(c), se(e){}
};
void leveldisplay(vector<int>*in_order, vector<int>*pre_order, int N)
{
int size, index;
queue<levelout>q;
cout << (*pre_order)[1];
q.push(levelout(1, N, 1, N));
while (!q.empty())
{
size = q.size();
while (size--)
{
index = findmidindex(in_order, (*pre_order)[q.front().prs], q.front().si, q.front().se);
if (index <= q.front().se)
{
if (index - q.front().si> 0)
{
cout << " " << (*pre_order)[q.front().prs + 1];
q.push(levelout(q.front().prs + 1, q.front().prs + index - q.front().si, q.front().si, index - 1));
}
if (q.front().se - index > 0)
{
cout << " " << (*pre_order)[q.front().prs + index - q.front().si + 1];
q.push(levelout(q.front().prs + index - q.front().si + 1, q.front().pre, index + 1, q.front().se));
}
}
q.pop();
}
}
cout << endl;
}
int main()
{
int N;
cin >> N;
vector<int>in_order(N + 1, NOExist), pre_order(N + 1, NOExist), post_order(N + 1, NOExist);
readln(&in_order, N + 1);
readln(&pre_order, N + 1);
readln(&post_order, N + 1);
if (N>0&&1==_in_pre_post(&in_order, &pre_order, &post_order, 1, N, 1, N, 1, N, N))
{
Display(&in_order, N);
Display(&pre_order, N);
Display(&post_order, N);
leveldisplay(&in_order, &pre_order, N);
}
else cout << "Impossible" << endl;
system("pause");
return 0;
}