PAT 1119 Pre- and Post-order Traversals [二叉树遍历][难]

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

 题目大意:假设一棵二叉树的所有关键字均是不同的正整数。给出前序遍历和后序遍历,如果中序是唯一的,那么就输出Yes,并且输出中序遍历;如果不唯一,那么就输出No,并且输出其中一种遍历序列即可。

 //啊啊,刚看到这个题就很懵,因为以前只知道前序和后序不能确定一棵二叉树,但是也没往深里想。

//不太会,放弃。

主要是因为没有中根无法区分左右子树,对于给的样例来说:

1 2 3 4 和后序2 4 3 1 它的中根可能有:2 1 3 4和2 1 4 3 两种。4无法确定是3的左节点还是右节点。

代码转自:https://www.liuchuo.net/archives/2484

#include <iostream>
#include <vector>
using namespace std;
vector<int> in, pre, post;
bool unique = 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
            unique = false;
        in.push_back(post[postRight]);//把根push了进去。
        getIn(i, preRight, postLeft + (i - preLeft - 1), postRight - 1);
        //一次处理之后就把根和左子树
    }
}
int main() {
    int n;
    scanf("%d", &n);
    pre.resize(n), post.resize(n);
    for (int i = 0; i < n; i++)
        scanf("%d", &pre[i]);
    for (int i = 0; i < n; i++)
        scanf("%d", &post[i]);
    getIn(0, n-1, 0, n-1);
    printf("%s\n%d", unique == true ? "Yes" : "No", in[0]);
    for (int i = 1; i < in.size(); i++)
        printf(" %d", in[i]);
    printf("\n");
    return 0;
}

 

//好难的这道题,不太会,还是需要复习。

posted @ 2018-10-11 09:41  lypbendlf  阅读(171)  评论(0编辑  收藏  举报