PAT 1020. Tree Traversals
PAT 1020. Tree Traversals
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
分析
首先后序遍历的最后一个数必定是根节点,然后在中序遍历中找的根节点,根节点前面的是该节点的左子树,后面的是右子树,再对左右子树递归的调用前面的步骤;
代码如下
#include<iostream>
#include<vector>
using namespace std;
vector<int> post,in,level(100000,-1);
void pre(int root,int s,int e,int index){
if(s>e) return ;
level[index]=post[root];
int i=s;
for(;i<=e;i++)
if(in[i]==post[root]) break;
pre(root-(e-i+1),s,i-1,index*2+1);
pre(root-1,i+1,e,index*2+2);
}
int main(){
int N; cin>>N;
post.resize(N); in.resize(N);
for(int i=0;i<N;i++) cin>>post[i];
for(int i=0;i<N;i++) cin>>in[i];
pre(N-1,0,N-1,0);
for(int i=0;i<level.size();i++)
if(level[i]!=-1)
i>0?cout<<" "<<level[i]:cout<<level[i];
return 0;
}
PAT 1020. Tree Traversals的更多相关文章
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- 【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 (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- 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 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 1020 Tree Traversals——PAT甲级真题
1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...
- PAT 1086 Tree Traversals Again
PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 1086 Tree Traversals Again[中序转后序][难]
1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...
随机推荐
- 堆排序的C实现
这几天有点抵触情绪,看过了快速排序还有一些别的东西,但是一点都不想写有点复杂的代码0 0拖到了今天终于写了前几天就应该自己写一下的堆排序,完全用C语言写的,下面把代码贴一下.很多地方写得并不好,不过已 ...
- CentOS下网卡启动、配置等ifcfg-eth0教程
步骤1.配置/etc/sysconfig/network-scripts/ifcfg-eth0 里的文件. CentOS6.4 下的ifcfg-eth0的配置详情: [root@Jeffery]# v ...
- GitHub中有关已建仓库及上传项目文件的删除
上传了项目,竟然发现找不到删除的地方,真是囧... 1. 已建仓库的删除 点击 settings,进入以下界面,点击箭头指向的按钮 进入以下界面 2. 某个文件的删除 直接点开文件,在右上角有个删除工 ...
- 规范化创建一个vs2017 Mvc框架项目
vs2107 + dapper + MiniUi 标准化分层封装使 3.1 规范化创建一个vs2017 Mvc框架项目 此时创建的项目勾选 添加单元测试. 添加一个类库,主要用于实体类操作,类库名称 ...
- 题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- SCRIPT70: 没有权限
主要原因:iframe安全而引发的问题,浏览器中js是没有垮域访问的权限的.如果用到iframe首先确保不垮域,或者不用iframe以绕开这个问题. 另外在jquery的早期版本中如:jquery-1 ...
- dubbo与springmvc的简单使用
什么是Dubbo? dubbo是阿里巴巴公司开源的高性能优秀服务框架,通过高性能的RPC(远程服务调用)实现服务的输入输出功能,可以与spring框架无缝整合: 传统的架构所有的模块都在一台服务器上, ...
- mongo 3.4分片集群系列之六:详解配置数据库
这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...
- 调度kettle使用taskctl我该怎么部署
转载自: http://www.taskctl.com/forum/detail_133.html 最近在QQ群看到有小伙伴在问用taskctl调度kettle,都要安装些什么呢?都支持哪些平台上的k ...
- 导入RPA应该了解的一些知识
上次概要介绍了下RPA的基本知识,这次说一下导入RPA时应该知道的一些知识点. RPA是为了实现用软件自动化代替在各个行业中用人操作电脑办公的这部分业务,即使是在多个应用之间需要共享数据才能完成的业务 ...