A1119 Pre- and Post-order Traversals (30 point(s))

先序+后序=中序

1. 原文

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:

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

Sample output 1:

1
2
Yes
2 1 6 4 7 3 5

Sample Input 2:

1
2
3
4
1 2 3 4
2 4 3 1

Sample output 2:

1
2
No
2 1 3 4

2. 解析

先序 根 左 右

后序 左 右 根

得到的树当左子树为空或右子树为空时,无法唯一

假设左子树为空时,不唯一

if(i-left-1==0) isunqiue=false;

  • 得到左子树

  •   int i=0;
          while(i<=prer&&pre[i]!=post[root-1]){
              i++;
          }
      int left=i-prel-1;
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59

    # <font size="4px">AC代码</font>

    ```c++
    #include<cstdio>
    #include<vector>
    using namespace std;
    int pre[40]={};
    int post[40]={};
    vector<int> in;
    bool isunique=true;
    void inorder(int prel,int prer,int root)
    {
    if (prel==prer)
    {
    in.push_back(pre[prel]);
    return;
    }
    int i=0;
    while(i<=prer&&pre[i]!=post[root-1]){
    i++;
    }
    int left=i-prel-1;
    if (i-prel-1>0)
    {
    inorder(prel+1,i-1,root-1-(prer-i+1));
    }else{
    isunique=false;
    }
    in.push_back(post[root]);
    inorder(i,prer,root-1);

    }
    int main()
    {
    int n;
    scanf("%d",&n);
    for (int i = 0; i < n; ++i)
    {
    scanf("%d",&pre[i]);
    }
    for (int i = 0; i < n; ++i)
    {
    scanf("%d",&post[i]);
    }
    inorder(0,n-1,n-1);
    if (isunique)
    {
    printf("Yes\n%d",in[0]);
    }else{
    printf("No\n%d",in[0]);
    }
    for (int i = 1; i < in.size(); ++i)
    {
    printf(" %d",in[i]);
    }
    printf("\n");
    return 0;
    }
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1119/
  • 发布时间: 2019-09-03 08:34
  • 更新时间: 2021-10-29 14:08
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!