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 分)的更多相关文章

  1. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  2. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  3. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  4. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  5. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  6. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. 1020 Tree Traversals (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  8. 1020 Tree Traversals (25分)思路分析 + 满分代码

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  9. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

随机推荐

  1. 工作采坑札记: 4. linux指定目录使用df和du的统计结果相差很大

    1. 背景 近日,线上的服务出现异常,调用服务返回的JSON格式数据不完整,导致客户端解析异常,因此记录了本次的填坑之旅(nnd)... 2. 排查过程 2.1 服务器分析 登录到服务所在linux服 ...

  2. laravel 加载指定版本的mongodb

    composer require jenssegers/mongodb:3.3 注意开启 php的mongodb的扩展 pecl install mongodb

  3. TP5 生成数据库字段 和 路由 缓存来提升性能

    关于使用tp5框架如何提升部分性能,框架中很多影响性能的问题在于,很多请求都要重新加载,如果能避免过度加载的问题,就能提升部分性能,所以我们通过缓存来实现这一功能,具体如下. 首先说明 如果是linu ...

  4. 43.QQ聊天软件GUI窗口编写

    QQ聊天软件代码功能编写 一,Tkinter聊天界面编写 1,聊天软件客户端界面开发-1 Tkinter的模块(“TK接口”)是标准的Python接口从Tk的GUI工具包 https://i.cnbl ...

  5. python知识储备目录

    1.with as 的实现原理 https://www.cnblogs.com/lice-blog/p/11581741.html 2.事务的四个隔离级别 3.rabitMQ 4.c3算法 5.wit ...

  6. 码云、coding拉取项目代码

    1.git init 2.git remote add origin "你的码云或coding项目地址(ssh或https)" 3.git pull origin master

  7. Python基础总结之第七天开始【总结字符串、列表、元组的常用方法】(新手可相互督促)

    前面的笔记说,python中的一切数据类型都是对象 我们在细化下就是:对象可由两部分组成:对象数据和对象方法 针对不同类型的数据对象,有不同的操作对象的方法. 那么我们开始看下字符串对象的常用方法: ...

  8. [转帖]Linux-Windows 端口转发 netsh 还有 rinetd

    Linux-Windows 端口转发 https://www.cnblogs.com/operationhome/p/11284559.html 之前自己学习过 netsh 也曾经用过frp 这次学习 ...

  9. 为什么fastjson字段为null时不输出空字符串?

    为什么fastjson字段为null时不输出空字符串? Map < String , Object > jsonMap = new HashMap< String , Object& ...

  10. (十)springMvc 校验

    目录 文章目录 目录 @[toc] springMvc 校验 准备 踩坑 配置校验器 将校验器注入到适配器中 在 pojo 中配置校验规则 在 controller 层进行检验 获取错误信息 将错误信 ...