PAT (Advanced Level) Practise 1119Pre- and Post-order Traversals (30)
来源:互联网 发布:女包淘宝网 编辑:程序博客网 时间:2024/05/16 11:46
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:71 2 3 4 6 7 52 6 7 4 5 3 1Sample Output 1:
Yes2 1 6 4 7 3 5Sample Input 2:
41 2 3 42 4 3 1Sample 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;}
- PAT (Advanced Level) Practise 1119Pre- and Post-order Traversals (30)
- PAT (Advanced Level) Practise 1119 Pre- and Post-order Traversals (30)
- 【PAT】【Advanced Level】1119. Pre- and Post-order Traversals (30)
- PAT(A) 1119 Pre- and Post-order Traversals (30)(数据结构)
- PAT--1119. Pre- and Post-order Traversals
- (PAT)1119. Pre- and Post-order Traversals
- 【PAT】1119. Pre- and Post-order Traversals
- PAT 1119. Pre- and Post-order Traversals (30)
- 【PAT】1119. Pre- and Post-order Traversals (30)
- PAT 1119. Pre- and Post-order Traversals (30)
- pat 1119. Pre- and Post-order Traversals (30)
- PAT A1119 pre- and post-order traversals(30)
- PAT A1119. Pre- and Post-order Traversals (30)
- PAT 1119. Pre- and Post-order Traversals (30)
- PAT A1119. Pre- and Post-order Traversals (30)
- PAT 1119. Pre- and Post-order Traversals (30) 用前序、后序求中序
- PAT 甲级 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30) PAT
- 【备份专题】备份软件分布式索引架构
- POJ-1273-Drainage Ditches(网络最大流)
- HDU 5876 Sparse Graph 补图BFS
- 给Java程序猿们推荐一些值得一看的好书
- 尚硅谷java学习笔记——1.java语言概述
- PAT (Advanced Level) Practise 1119Pre- and Post-order Traversals (30)
- 阻塞 非阻塞与同步 异步
- 【poj1962】 Corporative Network
- ucos学习4
- Java中构造器内部的多态方法的行为
- Content Provider
- 练习:超链接的单选效果 表格隔行变色
- 知识库构建-Knowledge Base
- Tkinter基础