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

题目大意:在二叉树中,其关键字是互不相通的正整数,给出其后根遍历(postorder)和中根遍历(inorder ),要求给出其层次遍历的顺序。

//其实没太做过二叉树的题目。果断放弃,查答案。代码来自:https://www.liuchuo.net/archives/2100

//不一定根节点就是n。

//因为后根遍历中最后一个节点一定是根节点。

#include <iostream>
#include <vector>
#include<stdio.h>
using namespace std;
vector<int> post, in, level(, -);//level是一个size为100000,初始化为-1
//因为N<=30,层数为30的,最多有2^30-1个节点。这好像也放不开啊。。
//root指向当前遍历段的根节点,开始下标(指向中根遍历),结束下标(指向中根遍历),层次遍历的下标。
void pre(int root, int start, int end, int index) {
if(start > end) return ;//递归出口,当没有左子树或者右子树时
int i = start;
while(i < end && in[i] != post[root]) i++;//while循环在中根遍历中找到根。 //后根遍历中最后一个点一定是整棵树的根,从而分为左子树与右子树。
level[index] = post[root];//当前层次遍历就是根节点。
pre(root - - end + i, start, i - , * index + );
//遍历左子树,index中存放的是层次遍历的结果。
pre(root - , i + , end, * index + );
//遍历右子树,根据二叉树的存储特性,左子树是2*当前+1,右子树是2*当前+2;
}
int main() {
int n, cnt = ;
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]);
pre(n-, , n-, );
for(int i = ; i < level.size(); i++) {
if (level[i] != -) {//index可能是-1,表示那个节点为空。
if (cnt != ) printf(" ");
printf("%d", level[i]);
cnt++;
}
if (cnt == n) break;
}
return ;
}

//另有中后遍历转前序遍历代码:

#include <cstdio>
using namespace std;
int post[] = {, , , , , };
int in[] = {, , , , , };
void pre(int root, int start, int end) {
if(start > end) return ;
int i = start;
while(i < end && in[i] != post[root]) i++;//在中序遍历中找到根节点
printf("%d ", post[root]);//打印根节点
pre(root - - end + i, start, i - );//遍历
pre(root - , i + , end);
} int main() {
pre(, , );
return ;
}

//感觉不要更牛一点。

PAT 1020 Tree Traversals[二叉树遍历]的更多相关文章

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

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

  2. PAT 1020. Tree Traversals

    PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...

  3. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

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

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

  5. 1020 Tree Traversals——PAT甲级真题

    1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...

  6. PAT Advanced 1020 Tree Traversals (25 分)

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

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

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

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

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

  9. PAT 1086 Tree Traversals Again

    PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...

随机推荐

  1. Delphi XE 10 跨平台三层数据库应用教程

    Delphi XE 10 跨平台三层数据库应用教程 前言: Delphi XE 开始越来越庞大,比经典的Delphi7难用,但依然是目前所有跨平台开发工具中开发效率最高.最容易上手的,其快速设计RAD ...

  2. 使用 github Pages 服务建立个人独立博客全过程

    你是否有这样子的需求,只是想简单的写写文章,记录下自己的学习心得.成长经历等,都是些文字内容,不需要配置使用数据库.不想购买服务器自己搭建站点,只是想安安静静的用比较舒服的方式来写篇文章. 静态博客就 ...

  3. jQuery队列(一)

    jQuery的队列依赖缓存机制事件,它同时是animate的基础. 它不像事件机制.缓存机制.回调机制一样有自己的命名空间,由于比较简单,所以直接挂在到$和jQuery对象上. 它提供的基础方法有: ...

  4. XCode 遇到的问题

    俗话说:工欲善其事必先利其器.抛弃了VS,投入XCode的怀抱.先不说两者的差距,还是先熟悉开发工具是关键.下面列出个人使用中遇到的一些问题. Problem1:修改Xcode字体颜色以及调整字体大小 ...

  5. sencha touch 侧边栏扩展(只隐藏不销毁)

    基于Ext.ux.MenuButton改造而来,和它不同的是,不会每次都去销毁侧边栏,只是单纯的隐藏,属性配置方面没啥区别,每次点击按钮显示时,会触发showMenu事件/方法 代码如下: /** * ...

  6. [转]Linux sendmail 详解

    Internet上最基本的服务,现在应该大部分人都有自己的邮箱吧,用的人多,但理解的人估计没多少,我自己以前也是常常用,但对其原理并不操心.今天就来操心下,进行个小总结 一.邮件服务的基本流程     ...

  7. C++ 标准输出cout与printf

    C++标准输出cout与printf都可以,printf用法更死板一些. #include <iostream> int main(int argc, char** argv) { usi ...

  8. springMVC去掉静态资源的拦截

    前端控制器的配置 <!-- springmvc的前端控制器 --> <servlet> <servlet-name>springMVC</servlet-na ...

  9. Scala日期处理

    计算时间间隔  val d = new java.text.SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new java.util.D ...

  10. TFS二次开发04——工作区(Workspace)和映射(Mapping)

    在前面几节介绍了怎样读取TFS服务器上的项目以及文件的信息,这一节将介绍怎么建立服务器和本地的映射(Mapping). 引用命名空间 usingMicrosoft.TeamFoundation.Cli ...