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

来源:互联网 发布:实木床 品牌 知乎 编辑:程序博客网 时间:2024/05/20 09:06

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


结题思路 :
题意要求我们根据前续遍历和后续遍历推断出中续遍历。
要求1:我们知道前续遍历与后续遍历不一定能确定中续遍历,题目要求我们对不能确定的生成一个可行解即可;
要求2:前续遍历MLR,后续遍历LRM,从遍历顺序中我们可以知道MLR中M作为跟节点,后面紧跟着他的一个字节点;同理,从LRM的根节点前面紧贴着他的一个字节点,我们可以通过这两个字节点是否一样来判断子节点的个数(题目中明确,数字没有重复);
要求3:一定要记得数据输出结束以后输出换行,貌似PAT上有些题目不换行也能通过。

程序步骤:
第一步、根据MLR跟LRM通过递归建树。
第二步、中续遍历新建的树,输出到vector中。
第三步、输出数组。


具体程序(AC)如下:

#include <iostream>#include <vector>#define maxInt 35using namespace std;struct node{    int value;    int left;    int right;};int n;vector<int> pre;vector<int> post;vector<int> result;node tree[maxInt];int count = 0;bool valid;void rebuild(int startPre, int endPre, int startPost, int endPost, int root){    if(startPre > endPre)        return;    if(pre[startPre] == post[endPost])//ambigulous    {        valid = false;        tree[count].value = pre[startPre];        tree[root].left = count++;        rebuild(startPre + 1, endPre, startPost, endPost - 1, tree[root].left);             }    else    {        tree[count].value = pre[startPre];        tree[count+1].value = post[endPost];        tree[root].left = count++;        tree[root].right = count++;        int i, j;        for(i = startPre; i <= endPre; ++i)            if(pre[i] == post[endPost])                break;        for(j = startPost; j <= endPost; ++j)            if(post[j] == pre[startPre])                break;        rebuild(startPre + 1, i - 1, startPost, j - 1, tree[root].left);                rebuild(i + 1, endPre, j + 1, endPost - 1, tree[root].right);    }}void inorder(int root){    if(root == -1)        return;    inorder(tree[root].left);    result.push_back(tree[root].value);    inorder(tree[root].right);}int main(){    cin>>n;    pre.resize(n);    post.resize(n);    result.clear();    for(int i = 0; i < n; ++i)        cin >> pre[i];    for(int i = 0; i < n; ++i)        cin >> post[i];    if(n == 0)        return 0;    if(n == 1)    {        cout << "Yes"<<endl<<pre[0]<<endl;          return 0;    }    for(int i = 0; i < maxInt; ++i)        tree[i].left = tree[i].right = -1;    tree[0].value = pre[0];    count++;    valid = true;    rebuild(1, n - 1, 0, n - 2, 0);    if(valid)        cout<<"Yes"<<endl;    else        cout<<"No"<<endl;    inorder(0);    if(n > 0)        cout << result[0];    for(int i = 1; i < n; ++i)        cout << " " << result[i];    cout<<endl;    return 0;}
0 0