1119. Pre- and Post-order Traversals (30)-PAT甲级真题(前序后序转中序)

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,然后随便输出遍历结果。

分析

  • 用一个unique标记是否唯一,unique=1表示唯一。
  • 当一个节点无法判断是根节点的左孩子或右孩子时,unique=false。
  • 当不唯一时,由于题目只要求输出一种可能性,可以假定这个不确定的孩子的状态是右孩子,然后问题的核心就是求解根节点和左右子树的划分了。
  • 用四个变量来记录范围,前序遍历的prel,prer,后序遍历的postl,postr。
  • 前序的开始和后序的结尾应该都是根节点,然后以后序根节点前一个节点作为参考,寻找这个节点在前序的位置,根据这个位置来划分左右孩子。

#include 
#include 

using namespace std;

vector in, pre, post;
bool flag = true;

void getIn(int preLeft, int preRight, int postLeft, int postRight) {
    if (preLeft == preRight) {
        in.push_back(pre[preLeft]);
        return;
    }
    if (pre[preLeft] == post[postRight]) {
        int i = preLeft + 1;
        while (i <= preRight && pre[i] != post[postRight - 1]) i++;
        if (i - preLeft > 1)
            getIn(preLeft + 1, i - 1, postLeft, postLeft + (i - preLeft - 1) - 1);//左子数
        else
            flag = false;  //区分不了左右子树
        in.push_back(post[postRight]);//根节点
        getIn(i, preRight, postLeft + (i - preLeft - 1), postRight - 1); //右子树
    }
}

int main() {
    int n;
    cin >> n;
    pre.resize(n), post.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> pre[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> post[i];
    }
    getIn(0, n - 1, 0, n - 1);
    if (flag)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    cout << in[0];
    for (int i = 1; i < in.size(); i++)
        cout << " " << in[i];
    cout << endl;
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注