引子

我之前对遍历推导这块没怎么动脑子,凭感觉。所以这题是先参考了百度经验,别笑,这个链接,我觉得讲得非常清楚。
https://jingyan.baidu.com/article/cdddd41cb8d79753ca00e144.html
读懂它的前提是,你静下心来,而不是像找规律一样,似是而非地懂了。那样毫无卵用。

通过推导它再举一反三推导其他题,你发现,思路是:

先通过后序的特点,找总的根节点。然后确定总根的右节点,一般是postorder最后一个节点(总根)的前一个节点post[4]=5。然后通过结合后序中序中相同的数字,如65,56确定好它们在一个最小子树中的位置,逐渐完善总根的右子树。然后找总根的左节点。一般是postorder中从后往前把右子树的节点数光的第一个节点。

则从PostOrder看,左子树的根节点序号为root的序号(比如0~5的5)-右子树节点的个数(2)-1=2。左子树根节点为post[2]=2~

int post[] = {3, 4, 2, 6, 5, 1};
int in[] = {3, 2, 4, 1, 6, 5};
很多文章说是root-(end-i+1)。但我比较能理解我自己的式子。当然它式子的end-i,这里的end是InOrder最后一位的序号,i是InOrder的根节点,相减是右子树节点数没错。


下面是根据上述数组推导前序的代码:

#include<cstdio>
using namespace std;
int post[] = {3, 4, 2, 6, 5, 1};
int in[] = {3, 2, 4, 1, 6, 5};
void pre(int root,int start,int end){if(start>end) return;int i=start;//找到根节点在InOrder中序号while(i<end&&in[i]!=post[root]) i++;printf("%d",post[root]);//打印根节点pre(root-(end-i)-1,start,i-1);//左根节点从postorder角度看,i-1从inorder角度看pre(root-1,i+1,end);//右根节点从postorder角度看,i+1从inorder角度看
}
int main(){pre(5,0,5);return 0;
}

正文

1020 Tree Traversals (25)(25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

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 postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. 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:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:

4 1 6 3 5 7 2


这题没啥头绪,先学习柳婼大神的
https://www.liuchuo.net/archives/2100

#include<iostream>
#include<vector>
using namespace std;
vector<int> post,in,level(100000,-1);
void pre(int root,int start,int end,int index){if(start>end) return;int i=start;//找到根节点在InOrder中序号while(i<end&&in[i]!=post[root])i++;// printf("%d",post[root]);//打印根节点level[index]=post[root];pre(root-(end-i)-1,start,i-1,2*index+1);//左根节点从postorder角度看,i-1从inorder角度看pre(root-1,i+1,end,2*index+2);//右根节点从postorder角度看,i+1从inorder角度看
}int main(){int n,cnt=0;cin>>n;post.resize(n);in.resize(n);for(int i=0;i<n;i++){scanf("%d",&post[i]);}for(int i=0;i<n;i++){scanf("%d",&in[i]);}pre(n-1,0,n-1,0);for(int i=0;i<level.size();i++){if(level[i]!=-1){if(cnt!=0) cout<<" ";cout<<level[i];cnt++;}if(cnt==n)break;}return 0;
}

//有机会带来自己的理解。

PAT甲级1020(附带前中序遍历の绝对干货)相关推荐

  1. 二叉树重建(前中序遍历求后序模板刘汝佳小白本)

    输入一棵二叉树的先序遍历和中序遍历序列,输出它的后序遍历序列. 输入: DBACEGF ABCDEFG BCAD CBAD 输出: ACBFGED CDAB #include<stdio.h&g ...

  2. 二叉树面试题:前中序求后序、中后序求前序

    在面试时,避免不了的会遇到一些数据结构的面试题,今天我们就来了解一下二叉树的经典面试题: 已知二叉树的前序遍历顺序为ABDCEGHF,中序遍历顺序为DBAGEHCF,求该二叉树的后序遍历. 还有: 已 ...

  3. PAT甲级1020 Tree Traversals:[C++题解]树的遍历、由中序序列和后序序列递归建树

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的后序遍历序列和中序遍历序列,让求层次遍历的序列. 分析: 后序遍历:先 左子树.右子树 ,最后再遍历根结点. 中序遍历:先左子树,再根 ...

  4. PAT甲级1020变体:已知二叉树层序+中序序列,求后序遍历序列

    PAT甲级1020变体:已知二叉树层序+中序序列,求后序遍历序列 题目 输入格式 输出格式 输入样例 输出样例 代码 题目 已知二叉树层序+中序序列,求后序遍历序列. 输入格式 第一行给出该二叉树的节 ...

  5. PAT甲级1138 Postorder Traversal:[C++题解]前序遍历和中序遍历建树

    文章目录 题目分析 题目链接 题目分析 做过前面几道题,发现这道题就是一道模板题,递归建树即可. 还是使用笔者熟悉的hash表来找根,进行优化. 请移步至笔者的另一篇文章:PAT甲级1020 Tree ...

  6. PAT甲级1130 Infix Expression:[C++题解]中缀表达式、二叉树中序遍历、dfs

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:本题是借助中缀表达式这个背景,考察二叉树的中序遍历.本题需要注意的地方是加括号. 左子树和右子树无脑加括号,只要不是叶结点. 所以写d ...

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

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

  8. PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...

  9. 反转二叉树 Java代码 (二叉树,中序遍历,层序遍历)【PAT甲级1102】

    输入样例: 8 1 - - - 0 - 2 7 - - - - 5 - 4 6 输出样例: 3 7 2 6 4 0 5 1 6 5 7 4 3 2 0 1 算法思路: 要将二叉树翻转,可以先按题意建好 ...

最新文章

  1. 预测----三个原则
  2. matlab绘图 subplot函数使用方法
  3. 2020-09-11
  4. 迈入现代 Web 开发(GMTC 2021 演讲全文)
  5. maven3安装和使用笔记
  6. Matlab代码提示“svmtrain已删除 请改用fitcsvm”,以及svmpredict没有返回结果label和精度accuracy的解决办法
  7. Firefox 的使用(firefox 拓展应用程序)
  8. python 06day --bootstrap框架使用及linux的磁盘管理
  9. Android 补间动画TranslateAnimation 位移效果
  10. springboot实现条形码_OkapiBarcode生成条形码
  11. QT随手记:解决opencv播放USB视频延迟、拖影的方法
  12. TypeScript Essential Notes 2 - ES6 Language Features
  13. windows远程连接mac
  14. arcscene如何制作三维真实场景
  15. 三大世界级难题,等你来解答
  16. mysql存储过程出参和入参_数据库mysql存储中的入参出参理解
  17. docker部署seaweedf
  18. JAVA 接口(interface)
  19. Rect电影项目 及 豆瓣Api最新接口
  20. 竖流式沉淀池集水槽设计计算_竖流式沉淀池计算说明

热门文章

  1. transaction缩写为什么是tx_TX Transaction locks常见的4种情况
  2. sys_guid() mysql写法_PostgreSQL Oracle 兼容性之 - sys_guid()
  3. mysql 跳过一个事物_MYSQL GTID跳过指定事务
  4. MFC的Dialogbox多行文本框(CEdit)有最大字符限制,默认最大显示长度
  5. oracle安装无响应,求教 pl/sql连接本机数据库是未响应问题
  6. mongorepository查询条件_Java操作MongoDB采用MongoRepository仓库进行条件查询 | 学步园...
  7. 04.自定义View(SlidingView仿QQ侧滑)
  8. GAN诞生记:最火的AI模型,来自一群博士的酒后争吵
  9. 荣之联生物领域再扩张 同清华大学共建生物云实验室
  10. pfSense添加子网的几种方式