PAT 甲级 1119. Pre- and Post-order Traversals (30)
来源:互联网 发布:天地图数据融合 编辑:程序博客网 时间:2024/06/05 08:56
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
#include <iostream> using namespace std; const int maxn = 31; int n, index = 0; int pre[maxn], post[maxn]; bool flag = true; struct Node { int data; Node *lchild, *rchild; } *root; Node *create(int preL, int preR, int postL, int postR) { if (preL > preR) return NULL; Node *node = new Node; node->data = pre[preL]; node->lchild = NULL; node->rchild = NULL; if (preL == preR) return node; int k = 0; for (k = preL + 1; k <= preR; k++) { if (pre[k] == post[postR - 1]) break; } if (k - preL > 1) { node->lchild = create(preL + 1, k - 1, postL, postL + k - preL - 2); node->rchild = create(k, preR, postL + k - preL - 1, postR - 1); } else { flag = false; node->rchild = create(k, preR, postL + k - preL - 1, postR - 1); } return node; } void inOrder(Node *node) { if (node == NULL) return; inOrder(node->lchild); if (index < n - 1) cout << node->data << " "; else cout << node->data << endl; index++; inOrder(node->rchild); } int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> pre[i]; for (int i = 0; i < n; ++i) cin >> post[i]; root = create(0, n - 1, 0, n - 1); if (flag) cout << "Yes\n"; else cout << "No\n"; inOrder(root); return 0; }
- PAT 甲级 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)-PAT甲级真题(前序后序转中序)
- PAT - 甲级 - 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 1119. Pre- and Post-order Traversals (30)
- 【PAT】【Advanced Level】1119. Pre- and Post-order Traversals (30)
- PAT 1119. Pre- and Post-order Traversals (30) 用前序、后序求中序
- 1119. Pre- and Post-order Traversals (30) PAT
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 1119. Pre- and Post-order Traversals (30)
- 3、单例模式(序列化与反序列化)
- 新手学Django大坑之加载静态图片
- 【Openjudge】英语数字转换器
- java Exception: NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory
- List add元素返回java.lang.NullPointerException
- PAT 甲级 1119. Pre- and Post-order Traversals (30)
- 程序的不同段的区别和作用
- unique-paths
- 在Windows下配置svn服务端钩子程序
- PAT 甲级 1120. Friend Numbers (20)
- 4、单例模式(静态代码块实现)
- 数据结构与算法之四(链表)
- PAT 甲级 1121. Damn Single (25)
- MongoDB权威指南--阅读笔记(一)