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

分析:根据序列建树,关键还是左右子树的划分,先序序列为N L R,后序为L R N,可以首先确定下根节点,后序中的R又可以分为L R N,因此后序的倒数第二个就是右子树的根节点,在先序序列中寻找该节点的位置就能确定左右子树,当\(pos = preL + 1\)时,就会出现不唯一的情况

#include<iostream>
#include<cstdio>
#include<vector>
#include<unordered_map>
#include<string>
#include<set>
#include<algorithm>
#include<cmath>
using namespace std;
const int nmax = 40;
int pre[nmax], post[nmax];
struct node{
int v;
node *lchild, *rchild;
};
typedef node* pnode;
bool uq = true;
pnode creat(int preL, int preR, int postL, int postR){
if(preL > preR)return NULL;
if(preL == preR){
pnode root = new node;
root->v = pre[preL];
root->lchild = root->rchild = NULL;
return root;
}//这是为了防止在寻找pos时发生数组越界
pnode root = new node;
root->v = pre[preL];
root->lchild = root->rchild = NULL;
int pos = preL + 1;
while(pre[pos] != post[postR - 1])pos++;
if(pos == preL + 1)uq = false;
int numleft = pos - preL - 1;
root->lchild = creat(preL + 1, pos - 1, postL, postL + numleft - 1);
root->rchild = creat(pos, preR, postL + numleft, postR - 1);
return root;
}
vector<int>ans;
void inOrder(pnode root){
if(root == NULL)return;
inOrder(root->lchild);
ans.push_back(root->v);
inOrder(root->rchild);
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i)scanf("%d", &pre[i]);
for(int i = 0; i < n; ++i)scanf("%d", &post[i]);
pnode root = creat(0, n - 1, 0, n - 1);
if(uq == true)printf("Yes\n");
else printf("No\n");
inOrder(root);
for(int i = 0; i < ans.size(); ++i){
if(i > 0)cout<<" ";
cout<<ans[i];
}
cout<<endl;//要输出这个换行符,否则会显示格式错误
return 0;
}

最新文章

  1. linux0.11改进之四 基于内核栈的进程切换
  2. python_编程规范
  3. underscore api
  4. (进阶篇)PHP+Mysql+jQuery找回密码
  5. HT for Web基于HTML5的图像操作(一)
  6. 如何将一个对象存到网页中并在js中使用
  7. JQ中 trigger()和triggerHandler()区别
  8. 学习android开发笔记
  9. 洛谷 P1118 数字三角形游戏 Label:dfs
  10. 【多线程】Java线程面试题 Top 50(转载)
  11. 关于jQuery中.attr()和.prop()的问题
  12. Watch gcc at ubuntu 12,See ELF file header
  13. PHP之验证码识别
  14. centos 下mysql操作
  15. Oracle EBS-SQL (SYS-18):检查系统安装的各个表是否打开(PJM%).sql
  16. 基数排序---Java实现+C++实现
  17. Labeling Balls(变种拓扑)
  18. HTML通过事件传递参数到js 二 event
  19. Linux安装mysql5.7
  20. java画按钮的边框

热门文章

  1. CF977C Less or Equal 题解
  2. java面向对象类的继承~ 匿名类 ;多态特性;强制类型转换
  3. SpringBoot单元测试demo
  4. 【九度OJ】题目1023:EXCEL排序 解题报告
  5. 【LeetCode】991. Broken Calculator 解题报告(Python)
  6. 【LeetCode】975. Odd Even Jump 解题报告(C++)
  7. 【LeetCode】836. Rectangle Overlap 解题报告(Python)
  8. 一行代码完成定时任务调度,基于Quartz的UI可视化操作组件 GZY.Quartz.MUI
  9. Ubuntu mininet+Ryu环境安装
  10. JavaScript交互式网页设计 • 【第6章 初识jQuery】