1006. Tree Traversals - Hard Version (35)

37 篇文章 0 订阅
26 篇文章 0 订阅

1006. Tree Traversals - Hard Version (35)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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)
除了第一个,其他代码均如下五、四、三、二

 

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
9月17日 17:48答案正确351006C++ (g++ 4.7.2)3308datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130417/17
1答案正确12561/1
2答案正确13089/9
3答案正确13081/1
4答案正确11801/1
5答案正确33082/2
6答案正确11802/2
7答案正确11802/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;
}


  
  

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
9月17日 15:00部分正确331006C++ (g++ 4.7.2)3712datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确118017/17
1答案正确13041/1
2答案正确13169/9
3答案正确13081/1
4答案正确13081/1
5答案正确37122/2
6答案正确11802/2
7内存超限  0/2
#include<iostream>   
#include<vector>   
#include<string> 
#include<queue>
/*
9
3 - 2 1 7 - 8 4 6
- 7 5 3 2 1 - 6 4
3 1 - - 7 - 6 - -
9
3 5 2 1 7 - 8 4 6
- 7 5 3 2 1 - 6 4
3 1 2 5 7 4 6 8 -
9
3 - 2 1 - - 8 4 6
- 7 5 3 2 1 - 6 4
3 1 2 5 7 4 6 8 -
2
- -
1 -
- 1
*/
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];/*0--in;1--pre*/
  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;
}

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
9月17日 13:40部分正确331006C++ (g++ 4.7.2)3384datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确138417/17
1答案正确13081/1
2答案正确23089/9
3答案正确13841/1
4答案正确13081/1
5答案正确33002/2
6答案正确13042/2
7运行超时  0/2
#include<iostream>   
#include<vector>   
#include<string> 
#include<queue>
/*
9
3 - 2 1 7 - 8 4 6
- 7 5 3 2 1 - 6 4
3 1 - - 7 - 6 - -
9
3 5 2 1 7 - 8 4 6
- 7 5 3 2 1 - 6 4
3 1 2 5 7 4 6 8 -
9
3 - 2 1 - - 8 4 6
- 7 5 3 2 1 - 6 4
3 1 2 5 7 4 6 8 -
2
- -
1 -
- 1
*/
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;
}

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
9月17日 08:31部分正确241006C++ (g++ 4.7.2)1308datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130017/17
1答案正确12521/1
2答案错误12520/9
3答案正确13001/1
4答案正确13001/1
5答案错误11800/2
6答案正确12482/2
7答案正确13082/2
#include<iostream>   
#include<vector>   
#include<string> 
#include<queue>
/*
9
3 - 2 1 7 - 8 4 6
- 7 5 3 2 1 - 6 4
3 1 - - 7 - 6 - -
9
3 5 2 1 7 - 8 4 6
- 7 5 3 2 1 - 6 4
3 1 2 5 7 4 6 8 -
9
3 5 - 1 7 - 8 4 6
- 7 5 3 2 1 - 6 -
3 1 - 5 7 - 6 8 -
2
- -
1 -
- 1
3
2 3 -
3 2 1
2 1 3
*/
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++)/*中根已知,但不知道放in_order的哪里*/
    {
      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;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值