友情提示:这题非常值得自己思考独立做出来,请反复确认后再往下拉

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:

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

题意:给出树的前序后序遍历,输出中序。不能确定时,任选一种输出

分析:和前中、后中一样,我们需要找到前后遍历的特殊点

我最先感觉,pre的最后一个(5)和post的第一个(2),分别代表mid中最右和最左

然后发现 如果pre的第一个(1) 和post的最后一个(1)相等,那么1是根节点

_(:з」∠)_接着就迫不及待开始做了,一顿骚操作以后我发现还做不了

再看的时候发现:虽然单看pre没办法 确定(2)是(1)的左子树还是右子树,但是(1)在post中的前一个是(3),那么(3)要么是(1)的左子树要么是(1)的右子树,而pre当中(2)再过去才是(3),所以说明,(2)是(1)的左子树,(3)是(1)的右子树。那么剩下的(4)(5)(6)(7)呢? 再确认,(2)和(3)分开以后,(4)(5)(6)(7)明显就只能是(3)的子树

有了这一点,就可以开始做了

用递归分治,对(3)的左右子树再分别判断,逐步缩小,直至确认。以下图做详细说明

1.首先我们发现post(1)  前一位是 3,对应到pre(3)当中pre(3)与pre(1)中有间隔。说明:(2)是(1)的左子树,(3)是(1)的右子树

2.开始递归,在橙色圈中我们可以发现,(5)是(3)的右子树,(4)是3的左子树

3.post(4)前一位(7)对应到pre当中与pre(4)中间仍然数字间隔,所以(6)是(4)的左子树,(7)是(4)的右子树

4.(1)的左子树(2),可以看出左边是(1)不能用,右边则都在(3)的范围内,所以(2)两个子树为空

5.(3)的右子树(5),同上,两个子树为空

那么,我们要怎么判断有没有唯一解呢?如果每一个非叶节点都像上面(1)(3)(4)一样,显然是有唯一解的,但是如果题中第二个例子的(3),它的子树(4)是与(3)紧连的,中间没有数字隔开,这是就无法判断是左子树还是右子树。所以只要当我们发现有某个非叶节点只有一个孩子的时候,就可以输出No了

代码:

#include<iostream>
using namespace std;
int pre[40]={0};
int post[40]={0};
int n,judge=1;
struct node {int data;struct node *lchild,*rchild;
};
int find(int a[],int data){for(int i=0; i<n; i++)if(a[i] == data) return i;
}
struct node *creat(struct node *root, int preStart, int preEnd, int postStart, int postEnd){if(pre[preStart] == post[postEnd]){root = new struct node();root->data = pre[preStart];} if(preStart == preEnd || postStart == postEnd) return root; int i = find(pre, post[postEnd - 1]);//i是root右孩子在pre的索引 int j = find(post, pre[preStart + 1]);//j是root左孩子在post的索引    if(i - preStart >= 2) { root->lchild = creat(root->lchild, preStart+1, i-1, postStart, j);root->rchild = creat(root->rchild, i, preEnd, j+1, postEnd-1);}else{//其实只剩一种可能,即 i == preStart+1 judge=0;root->rchild = creat(root->rchild, i, preEnd, j+1,postEnd-1);}return root;
}
void midOut(struct node* root){static int cnt=0;if(root->lchild) midOut(root->lchild);if(root) cnt == 0 ? cout<< root->data : cout<<" "<<root->data;cnt++;if(root->rchild) midOut(root->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];struct node *root;root = creat(root, 0, n-1, 0, n-1);judge==1? cout<<"Yes"<<endl:cout<<"No"<<endl;midOut(root);cout<<endl;
}

结尾:这题感觉有些可惜,因为前面想了很久,中间试过很多“奇思妙想”的方法,当我想到先确认根再确认左右子树时,预估代码要写八九十行而且很繁琐,就忍不住看了别人的题解。 结果发现就是这个思路 _(:з」∠)_。  不过那时候没想到递归,还在扑哧扑哧上下左右想着点,所以弄的很复杂。  不过人家直接数组就记录了mid,然后直接输出,感觉还是很炫酷。

转载于:https://www.cnblogs.com/childwang/p/8280262.html

1119. Pre- and Post-order Traversals (30)相关推荐

  1. PAT甲级1119 Pre- and Post-order Traversals (30分):[C++题解]暴搜dfs、前序遍历和后序遍历求中序遍历

    文章目录 题目分析 题目链接 题目分析 分析 给了前序遍历和后序遍历,能够确定根结点,但是左子树和右子树的长度是不确定的.这里采用的解决方案是枚举左子树的结点个数,其实右子树的结点个数也确定了.对于每 ...

  2. 1119 Pre- and Post-order Traversals (30 分)【难度: 难 / 知识点: 树的构建】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805353470869504 难,未完成,有时间补题.

  3. 数据库中各表关联图及其说明_如何在图中思考:图论及其应用的说明性介绍

    数据库中各表关联图及其说明 by Vardan Grigoryan (vardanator) 由Vardan Grigoryan(vardanator) 如何在图中思考:图论及其应用的说明性介绍 (H ...

  4. Zuul:Pre和Post过滤器(下)

    可以看到所有的请求都会经过他,那么我们现在要对这个请求做一个权限的校验,加入没有Zuul这个服务,那你Server A/B/C,都要校验一次,这个每个服务都要做一次,所以权限校验我们可以放到Zuul统 ...

  5. 30 分钟未付款取消订单,怎么做?

    点击上方" 码农编程进阶笔记 ",选择"置顶或者星标" 文末有干货,每天定时与您相约! 第一次亲密接触 问题:我这边有个需求,用户下单后 30 分钟如果没付款就 ...

  6. 刷PAT甲级的各题思路、细节以及遇到的问题记录

    1001 A+B Format (20分) 因为一定会用到字符串,而string非常好用,但是用的时候一定要注意不能越界访问,否则会在运行时出现abort() has been called. 100 ...

  7. 【PAT甲级】A1101-A1155刷题记录

    文章目录 (递推) A1101 Quick Sort (25 分) 0.23 (静态二叉树+遍历) A1102 Invert a Binary Tree (25 分) 0.51 (数学问题) A110 ...

  8. 消解原理推理_什么是推理统计中的Z检验及其工作原理?

    消解原理推理 I Feel: 我觉得: The more you analyze the data the more enlightened, data engineer you will becom ...

  9. highlight.js 语法高亮,让你的页面更美观~

    在网页开发过程中,经常会用到代码的展示,而不同颜色的代码,可以让页面看起来更直观,也更美观. highlight.js是一款基于JavaScript的语法高亮库,目前支持125种编程语言,有63种可供 ...

最新文章

  1. LNMP架构——OpenResty实现缓存前移(到达Nginx前端层面)
  2. Javascript 中 apply、call、bind
  3. 把jpg转换成pdf软件
  4. iOS 采集音视频及写入文件
  5. 逻辑综合——优化电路
  6. 请问这博客能有几种方便写法?
  7. winform Outlookbar
  8. 三桥君:如何把SQL Server的数据库导为sql文件
  9. Java开发必看!java登录界面代码
  10. 红帽linux64系统下载,红帽rhel6.5下载
  11. matlab中uigetfile命令的应用
  12. transition transform属性造成文字抖动及模糊的解决方法
  13. 打造卓越的 Android 游戏体验
  14. 2021年危险化学品经营单位主要负责人考试及危险化学品经营单位主要负责人考试试卷
  15. SQL数据库损坏及恢复分析
  16. web前端图片极限优化策略
  17. kali linux改root密码
  18. I。quadratic equation 山东第八届省赛
  19. 电子计算机主机房国标,根据国标GB50174-93《电子计算机机房设计规范》.ppt
  20. 关于AM5728评估板

热门文章

  1. mysql infile local,MySQL:启用LOAD DATA LOCAL INFILE
  2. Maven配置_01
  3. 深入理解Tomcat和Jetty源码之第二篇servlet规范和servlet容器
  4. esp mounter pro_对比 | 以大欺小?剑指宋Pro和哈弗H6,欧尚X7的黑马潜质从何而来?...
  5. linux系统运行pbs出现ntf,Linux系统启动故障修复
  6. wsld2java axis_Weblogic+axis2安装
  7. 计算机命令秒退,Win10专业版下ping命令闪退自动关闭的解决办法
  8. 2021计算机专业考408的学校,2021考研:计算机考研408是什么?统考学校有哪些?...
  9. 21天学MySQL_SQL21天自学通.pdf
  10. 小猴吃桃matlab,看图写话:小猴吃桃精彩选篇