PAT_A 1119. Pre- and Post-order Traversals (30)

来源:互联网 发布:淘宝网开店 编辑:程序博客网 时间:2024/06/14 04:20

1119. Pre- and Post-order Traversals (30)

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line “Yes” if the tree is unique, or “No” if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input 1:

7
1 2 3 4 6 7 5
2 6 7 4 5 3 1

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4

  • 分析
    考二叉树的几种遍历方式

  • code

#include<iostream>#include<vector>using namespace std;#define N 3577struct _tree{  int left;  int right;  int value;};int pre[N];int post[N];_tree tree[N];bool unique=true;vector<int> in;void inTrave(int child){  if(child==0)  {    return;  }//  in.push_back(child);  inTrave(tree[child].left);  in.push_back(child);  inTrave(tree[child].right);}void deTrave(int preBegin,int preEnd,int postBegin,int postEnd,int root){  if(preBegin>preEnd)    return;  if(postBegin>postEnd)    return;  if(pre[preBegin]==post[postEnd])  {    unique=false;    tree[root].value=root;    tree[root].left=pre[preBegin];    deTrave(preBegin+1,preEnd,postBegin,postEnd-1,pre[preBegin]);  }  else   {    tree[root].value=root;    tree[root].left=pre[preBegin];    tree[root].right=post[postEnd];    int i=0;    int j=0;    for(i=postBegin;i<=postEnd;i++)    {      if(pre[preBegin]==post[i])        break;    }    for(j=preBegin;j<=preEnd;j++)    {      if(post[postEnd]==pre[j])        break;    }    //匹配左侧    deTrave(preBegin+1,j-1,postBegin,i-1,pre[preBegin]);    deTrave(j+1,preEnd,i+1,postEnd-1,post[postEnd]);  }}int main(){  int count=0;  cin>>count;  //pre[0]=count;  for(int i=1;i<=count;i++)  {    cin>>pre[i];    pre[0]++;  }  post[0]=count;  for(int i=1;i<=count;i++)  {    cin>>post[i];  }  if(count==0)    return 0;  deTrave(2,pre[0],1,post[0]-1,pre[1]);  if(unique==true)    cout<<"Yes"<<endl;  else    cout<<"No"<<endl;  inTrave(pre[1]);  int i=0;  for(i=0;i<in.size()-1;i++)  {    cout<<in.at(i)<<" ";  }  cout<<in.at(i)<<endl;  return 0;}/* * 题目说明: * * *  考察二叉树的遍历问题 *     NLR,LRN,LNR之间的关系,以及对三种遍历的深刻理解 *     NLR LRN 如果我们将 LR RL视为一组即都是他们的孩子,则此时NLR LRN是对称的 *     一般我们对遍历前序能很快写出来,同样对于后序,我们从右侧,把R当做L,L当做R,那么此时 *     后序的也就能很快书写出来。 *    通过NLR和LRN我们能找到共同的N,并找到它的左右孩子,当它没有左右孩子时,解递归到那一层时, *    就只剩下该节点了。 *    接着我们要在N节点下的左右孩子下分别独立的递归下去,直到其中一个没有数据 * * *    NLXXXXXXXRXXXXXXX *    YYYYLYYYYYYYYYYYRN * * *    左子树递归部分 *    LXXXXXXXXXXXXXXX *    YYYYL *    右子树递归部分 *    RXXXXXXX *    YYYYLYYYYYYYYYYYR *    . *    . *    . * *    这其实是对我们遍历时递归的一个解递归 *     * *    关于二义性问题 *     *    每一个递归部分 *    我们通过NLR的L该值为x *    LRN的R值为y, *    通过x,y确定N的左右孩子 *    当x==y时,我们无法确定该值是左还是右 * */
0 0
原创粉丝点击