PAT (Advanced Level) Practise 1119Pre- and Post-order Traversals (30)

来源:互联网 发布:女包淘宝网 编辑:程序博客网 时间:2024/05/16 11:46

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

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Special
作者
CHEN, Yue

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:
71 2 3 4 6 7 52 6 7 4 5 3 1
Sample Output 1:
Yes2 1 6 4 7 3 5
Sample Input 2:
41 2 3 42 4 3 1
Sample Output 2:
No2 1 3 4
给出先序遍历和后序遍历,询问树是否唯一,并输出。
#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)#define loop(i,j,k) for (int i = j;i != -1; i = k[i])#define lson x << 1, l, mid#define rson x << 1 | 1, mid + 1, r#define ff first#define ss second#define mp(i,j) make_pair(i,j)#define pb push_back#define pii pair<int,LL>#define in(x) scanf("%d", &x);using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-4;const int INF = 0x7FFFFFFF;const int mod = 998244353;const int N = 1e5 + 10;int n, a[N], b[N];int rt, ch[N][2], flag = 0;int solve(int l, int r, int ll, int rr){  if (l > r) return 1;  if (a[l] != b[rr]) return 0;  int res = 0;  rep(i, l, r)  {    int L = solve(l + 1, i, ll, ll + i - l - 1);    int R = solve(i + 1, r, ll + i - l, rr - 1);    res += L * R;  }  return res;}bool get(int &x, int l, int r, int ll, int rr){  if (l > r) return true;  if (a[l] != b[rr]) return false;  x = a[l];  rep(i, l, r)  {    if (get(ch[x][0], l + 1, i, ll, ll + i - l - 1) && get(ch[x][1], i + 1, r, ll + i - l, rr - 1)) return true;  }  return false;}void dfs(int x){  if (!x) return;  dfs(ch[x][0]);  printf("%s%d", flag ? " " : "", x); flag = 1;  dfs(ch[x][1]);}int main(){  in(n);  rep(i, 1, n) in(a[i]);  rep(i, 1, n) in(b[i]);  printf("%s\n", solve(1, n, 1, n) > 1 ? "No" : "Yes");  get(rt = 0, 1, n, 1, n); dfs(rt); putchar(10);  return 0;}


0 0