PAT甲級 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,4,8,5,3,6,7 。後序都是:8,4,5,2,6,7,3,1 。但是中序卻有兩種不同的情況:左圖(8,4,2,5,1,6,3,7)、右圖(4,8,2,5,1,6,3,7)。

對於一個非葉節點,如果前序遍歷的後一個結點,和後序遍歷的前一個結點不同,說明可以分爲左右孩子。

而且後續遍歷中,如圖中的結點①,左孩子的子結點有:④⑤⑧,右孩子的子結點有:⑥⑦,從後序中可以明顯看出來②③恰好將④⑤⑧和⑥⑦分開(其實前序也可以)。

根據這個特性直接搜中序遍歷序列。

代碼

#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
const int MAXN = 30 + 5;
const int MAXM = 100000 + 5;
const int INF = 0x7f7f7f7f;
template <class XSD> inline XSD f_min(XSD a, XSD b) { if (a > b) a = b; return a; }
template <class XSD> inline XSD f_max(XSD a, XSD b) { if (a < b) a = b; return a; }
int n, m;
int pre[MAXN], post[MAXN], pos[MAXN];
int ans[MAXN], cnt;

void Getdata(){
    cnt=0;
    for(int i=1; i<=n; i++) scanf("%d", &pre[i]);
    for(int i=1; i<=n; i++) scanf("%d", &post[i]), pos[post[i]]=i;///post[]中每個數的位置
}
bool BuildTree(int l1, int r1, int l2, int r2){///pre post
    if(l1>r1) return false;
    if(l1==r1){
        ans[cnt++]=pre[l1];
        return true;
    }
    int pos1=pos[pre[l1+1]];///後序中的下一個
    int res=1;
    res &= BuildTree(l1+1, l1+1+pos1-l2, l2, pos1);
    ans[cnt++] = pre[l1];
    res &= BuildTree(l1+2+pos1-l2, r1, pos1+1, r2-1);
    return res;

}
void Solve(){
    bool ys=BuildTree(1, n, 1, n);

    printf("%s\n", ys==true?"Yes":"No");
    for(int i=0; i<cnt; i++) printf("%d%c", ans[i], i==(cnt-1)?'\n':' ');
}
int main(){
    while(~scanf("%d", &n)){
        Getdata();
        Solve();
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章