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<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int pre[], post[], N;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data;
}node;
int exam(int preL, int preR, int postL, int postR){
if(preL > preR && postL > postR)
return ;
if(pre[preL] != post[postR])
return ;
int len = preR - preL;
int ans = ;
for(int i = ; i <= len; i++){
ans += exam(preL + , preL + i, postL, postL - + i) * exam(preL + i + , preR, postL + i, postR - );
}
return ans;
}
int create(int preL, int preR, int postL, int postR, node* &root){
if(preL > preR && postL > postR){
root = NULL;
return ;
}
if(pre[preL] == post[postR]){
root = new node;
root->data = pre[preL];
root->lchild = NULL;
root->rchild = NULL;
}else{
return ;
}
int ans = ;
int len = preR - preL;
for(int i = ; i <= len; i++){
ans = create(preL + , preL + i, postL, postL - + i, root->lchild) && create(preL + i + , preR, postL + i, postR - , root->rchild);
if(ans != )
return ;
}
return ans;
}
vector<int> visit;
void preOrder(node* root){
if(root == NULL)
return;
preOrder(root->lchild);
visit.push_back(root->data);
preOrder(root->rchild);
} int main(){
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%d", &pre[i]);
}
for(int i = ; i <= N; i++){
scanf("%d", &post[i]);
}
int ans = exam(, N, , N);
node* root = NULL;
create(, N, , N, root);
preOrder(root);
if(ans == )
printf("Yes\n");
else printf("No\n");
for(int i = ; i < visit.size(); i++){
if(i == visit.size() - )
printf("%d\n", visit[i]);
else printf("%d ", visit[i]);
}
return ;
}

总结:

1、检验的方法:使用前序、中序递归建立二叉树的方法差不多。传入前序区间和后序区间之后,由前序和后序都可以确定树根。该序列的根合法的情况有:传入区间为空(即空树); 前序确定的根和后序确定的根相同。 不合法的情况:前序与后序确定的树根不同。   然后将该序列划分为左右子树递归判断。有多种划分方法,需要循环。比如序列长为3,则可划分左右子树为(左0, 右3)  (左1, 右2)  (左2,右1)  (左3,右0)

2、需要注意的是,只有当该树的树根合法、左子树与右子树的划分合法,才能构成合法二叉树。划分种类数:左子树个数乘右子树个数。

3、递归建树则对上面的函数稍加改造即可, 核心方法就是找到根的序号并建立新节点存储根。

A1119. Pre- and Post-order Traversals的更多相关文章

  1. Construct a tree from Inorder and Level order traversals

    Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...

  2. [LeetCode] Rank Scores 分数排行

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  3. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  4. ASP.NET MVC : Action过滤器(Filtering)

    http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...

  5. HDU 1160 FatMouse's Speed

    半个下午,总算A过去了 毕竟水题 好歹是自己独立思考,debug,然后2A过的 我为人人的dp算法 题意: 为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点 本着“指 ...

  6. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  7. Spring Cloud Zuul 限流详解(附源码)(转)

    在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选择,只需要编写一个过滤器就可以了,关键在于如何实现限流的算法. ...

  8. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  9. LeetCode: Recover Binary Search Tree 解题报告

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  10. [LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal

    Pre: node 先,                      Inorder:   node in,           Postorder:   node 最后 PreOrder Inorde ...

随机推荐

  1. 动态SQL2

    set标签 存放修改方法,我们之前写的更新方法是全字段的更新,或者是指定字段的更新,现在我想实现一个新功能,传入的Employee包含什么字段,数据库就更新相对应的列值: 如果我们啥也不做直接上< ...

  2. 手机端图像编辑上传-cropper

    编辑头像,实现相册,照像功能,并能缩放裁剪功能,可自定义UI,引用'cropper.js', 'exif.js' /*初始化裁剪插件*/ var screenWidth = $(window).wid ...

  3. 关于controller的书写

    private Logger log = LoggerFactory.getLogger(ReportFormController.class); // 读取配置文件 ResourceBundle r ...

  4. 使用synchronized 实现ReentrantLock(美团面试题目)

    刚看到这个题目的时候无从下手,因为觉得synchronized和lock在加锁的方式上有很大不同,比如,看看正常情况下synchronized时如何加锁的. 方式一: public synchroni ...

  5. easyui datagrid动态修改editor时动态绑定combobox的数据

    需求在 datagrid 编辑框中开启一个combobox  ,但是里面的数据需要开启的时候才会知道,数据会根据其他因数变更 参考原文 :http://blog.csdn.net/donggua369 ...

  6. Python——Flask框架——模板

    一.渲染模板 render_template 函数把Jinja2模板引擎集成到程序中 二.Jinja2变量过滤器 过滤器名 说明 safe 渲染值是不转义 capitalize 把值得首字母转换成大写 ...

  7. Web API2 使用默认Identity

    当您选择个人账户在Web API项目模板,项目包含一个令牌授权服务器验证用户凭证和问题.下面的图显示了相同的凭证流的Web API组件. 发送一个未经授权的请求 首先,运行应用程序并单击按钮调用的AP ...

  8. hdu-4300(kmp或者拓展kmp)

    题意:乱七八糟说了一大堆,就是先给你一个长度26的字符串,对应了abcd....xyz,这是一个密码表.然后给你一个字符串,这个字符串是不完整的(完整的应该是前半部分是加密的,后半部分是解密了的),然 ...

  9. python数学库math模块

    函数 数学表示 含义 .pi 圆周率π π的近似值,15位小数 .e 自然常数 e e的近似值,15位小数 ceil(x) [x] 对浮点数向上取整 floor(x) [x] 对浮点数向下取整 pow ...

  10. GCD HDU - 2588

    输入 N 和 M (2<=N<=1000000000, 1<=M<=N), 找出所有满足1<=X<=N 且 gcd(X,N)>=M 的 X 的数量. Inpu ...