PAT Advanced 1020 Tree Traversals (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 (≤), 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
主要考查,中后遍历转前序,前中遍历建树,层序遍历树
#include <iostream>
#include <vector>
#include <queue> using namespace std; struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
}; vector<int> pre,in,post,ans;
queue<TreeNode*> que; void preOrder(int root,int start,int end){
if(start>end) return;
int i=;
while(i<=end&&in[i]!=post[root]) i++;
pre.push_back(post[root]);
preOrder(root--end+i,start,i-);
preOrder(root-,i+,end);
} TreeNode* buildTree(int root,int start,int end){
if(start>end) return NULL;
int i=;
TreeNode *t=new TreeNode();
while(i<=end&&in[i]!=pre[root]) i++;
t->val=pre[root];
t->left=buildTree(root+,start,i-);
t->right=buildTree(root++i-start,i+,end);
return t;
} void levelOrder(TreeNode *tree){
que.push(tree);
while(!que.empty()){
TreeNode *tmp=que.front();
ans.push_back(tmp->val);
que.pop();
if(tmp->left!=NULL) que.push(tmp->left);
if(tmp->right!=NULL) que.push(tmp->right);
} }
int main()
{
int N;
scanf("%d",&N);
post.resize(N);in.resize(N);
for(int i=;i<N;i++) scanf("%d",&post[i]);
for(int i=;i<N;i++) scanf("%d",&in[i]);
preOrder(N-,,N-);
TreeNode *tree=buildTree(,,N-);
levelOrder(tree);
for(int i=;i<ans.size();i++)
if(i!=ans.size()-) cout<<ans[i]<<" ";
else cout<<ans[i];
system("pause");
return ;
}
查看柳神博客,对代码修改,实际可以建立一个索引,这样就可以直接得到先序遍历的结果:
#include <iostream>
#include <vector>
#include <algorithm> using namespace std;
struct node{
int index;
int val;
};
bool cmp(node n1,node n2){
return n1.index<n2.index;
}
vector<int> in,post;
vector<node> ans; void preOrder(int root,int start,int end,int index){
if(start>end) return;
int i=;
while(i<=end&&in[i]!=post[root]) i++;
ans.push_back({index,post[root]});
preOrder(root--end+i,start,i-,*index+);
preOrder(root-,i+,end,*index+);
}
int main()
{
int N;
scanf("%d",&N);
post.resize(N);in.resize(N);
for(int i=;i<N;i++) scanf("%d",&post[i]);
for(int i=;i<N;i++) scanf("%d",&in[i]);
preOrder(N-,,N-,);
sort(ans.begin(),ans.end(),cmp);
for(int i=;i<N;i++)
if(i!=N-) printf("%d ",ans[i].val);
else printf("%d",ans[i].val);
system("pause");
return ;
}
PAT Advanced 1020 Tree Traversals (25 分)的更多相关文章
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
随机推荐
- [Tensorflow] 使用 Mask_RCNN 完成目标检测与实例分割,同时输出每个区域的 Feature Map
Mask_RCNN-2.0 网页链接:https://github.com/matterport/Mask_RCNN/releases/tag/v2.0 Mask_RCNN-master(matter ...
- vue项目富文本编辑器vue-quill-editor之自定义图片上传
使用富文本编辑器的第一步肯定是先安装依赖 npm i vue-quill-editor 1.如果按照官网富文本编辑器中的图片上传是将图片转为base64格式的,如果需要上传图片到自己的服务器,需要修改 ...
- Mybatis插件之Mybatis-Plus的实体类注解篇
主要注释到实体类上的注解: @TableName(value = …) 当数据库名与实体类名不一致或不符合驼峰命名时,需要在此注解指定表名 @TableId(type = …) 指定实体类的属性为对应 ...
- 使用Dreamweaver制作简单网站
上课过程中有的同学反应没有听懂,特写此博客,将dreamweaver使用过程,细化到每一步,跟着做就行. 一.安装dreamweaver. 1.dreamweaver免安装版下载地址 链接:https ...
- [LuoguP3064][USACO12DEC]伊斯坦布尔的帮派Gangs of Istanbull(加强版)_线段树_贪心
伊斯坦布尔的帮派Gangs of Istanbull 题目链接:https://www.luogu.org/problem/P3064 数据范围:略. 题解: 这个题其实分为两问,第一问是$YES$. ...
- java知识随笔整理-数据库的临时表
1.创建临时表的方法 方法一.select * into #临时表名 from 你的表; 方法二. create table #临时表名(字段1 约束条件,字段2 约束条件,.....)create ...
- spring mvc 的配置 及interceptor filter listener servlet 配置
创建 三个类 分别实现 Filter ServletContextListener HttpServlet 在springboot 启动类中@bean加入 2 ,实现 ServletContext ...
- @WebServlet注解
@WebServlet("/LoginServlet") jsp页面: <form action="LoginServlet" method = &quo ...
- Eureka【故障演练分析】
1.应用服务启动前不可用 假设eureka server服务在client应用服务启动之前挂掉,或者没有启动,这时应用服务依然可以正常启动,但是会有报错信息: 2019-10-13 14:40:41. ...
- Thinkphp解决phpExcel导出数据量大导致内存溢出
工作需要导出几万的数据量.操作比较频繁.之前数据在七八千是数据导出很慢.phpExcel是方便但是性能一般.现在改为使用csv导出数据:可以缓解内存压力,一次导出两三万是没问题的.当然服务器内存给力, ...