[PAT]1119 Pre- and Post-order Traversals (30 分)(样例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:

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

 二、解析

题目大意是,给出一个二叉树的前序遍历、后序遍历,求是否可以得到一个唯一确定的二叉树,如果可以输出Yes,并给出中序遍历序列。如果不可以输出No,并给出任一个中序遍历。

阶梯思路就不给出了,只给出我样例1没过的原因吧。

我第一次做时样例1没过,显示"运行时错误",后来发现是若有n个结点,输入的结点序号并不一定是1、2、3、4、5、6、、、n,在申请数组空间记录结点时需要尽量大的空间。如下代码申请的空间太小,所以样例1过不了。

 nodes.resize(n+5);

改为如下代码即可:

 nodes.resize(n+10000);

下面是AC代码:

#include <bits/stdc++.h>

using namespace std;

struct node {
    int left;
    int right;
    node() {
        this->left = -1;
        this->right = -1;
    }
};
int uni;
vector<int> pre;
vector<int> post;
vector<node> nodes;
vector<int> ans;

int getAns(int prel, int prer, int postl, int postr) {
    if((prel>prer)||(postl>postr)) {
        return -1;
    }
    int mid = pre[prel];

    if(prel == prer) {
        nodes[mid].left = -1;
        nodes[mid].right = -1;
        return mid;
    } else {
        if(pre[prel+1] == post[postr-1]) {
            uni = 0;
            nodes[mid].right = getAns(prel+1, prer, postl, postr-1);
            nodes[mid].left = -1;
        } else {
            int prei;
            int posti;
            prei = find(pre.begin(), pre.end(), post[postr-1])-pre.begin();
            posti = find(post.begin(), post.end(), pre[prel+1])-post.begin();

            nodes[mid].left = getAns(prel+1, prei-1, postl, posti);
            nodes[mid].right = getAns(prei, prer, posti+1, postr-1);
        }
        return mid;
    }
}

void inOrder(int n) {
    if(n==-1) {
        return ;
    }

    inOrder(nodes[n].left);
    ans.push_back(n);
    inOrder(nodes[n].right);
}

int main() {
    int n;
    uni = 1;
    cin>>n;
    nodes.resize(n+10000);

    for(int i=0; i<n; i++) {
        int a;
        cin>>a;
        pre.push_back(a);
    }

    for(int i=0; i<n; i++) {
        int a;
        cin>>a;
        post.push_back(a);
    }

    getAns(0, pre.size()-1, 0, post.size()-1);

    inOrder(pre[0]);

    if(uni) {
        cout<<"Yes"<<endl;
    } else {
        cout<<"No"<<endl;
    }

    for(int i=0; i<ans.size()-1; i++) {
        cout<<ans[i]<<" ";
    }
    cout<<ans.back()<<endl;
    return 0;
}

 

posted on 2019-08-15 10:54  刘好念  阅读(7)  评论(0)    收藏  举报  来源