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. 《转》Python学习(18)-python函数(二)

    转自 http://www.cnblogs.com/BeginMan/p/3173328.html 一.装饰器(decorators) 装饰器的语法以@开头,接着是装饰器函数的名字.可选参数. 紧跟装 ...

  2. Sencha Touch 实战开发培训 视频教程 第二期 第五节

    2014.4.16 晚上8:20分开课. 本节课耗时没有超出一个小时,主要讲解了Sencha Touch 结合百度地图的用法. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: ...

  3. [原]VNC client 图形化连接 linux

    1. #  vncserver     (启动vncserver,配置密码) 2. vim /root/.vnc/xstartup  (在最后一排添加gnome-session & ) vim ...

  4. vue组件定义方式

    一.全局组件 <div id="box"> {{msg}} <my-aaa></my-aaa> </div> var Home = ...

  5. Android.mk 用法介绍

    一个Android.mk file用来向编译系统描述你的源代码.具体来说:该文件是GNU Makefile的一小部分,会被编译系统解析一次或多次.你可以在每一个Android.mk file中定义一个 ...

  6. 关于sizeof和strlen

    已知 char *str1="absde"; char str2[]="absde"; char str3[8]={'a',}; char ss[] = &qu ...

  7. C# AES 加密与解密

    AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码,返回密文 /// <summary> /// AES 算法加密(ECB模式) 将明文加密,加密后进行base64编码 ...

  8. Windows Server 2008 R2之三管理活动目录数据库

    活动目录数据库包括数据库文件NTDS.dit和日志文件.考虑到最佳性能,在生产环境推荐将日志文件和数据库文件在单独的硬盘驱动器中或RAID中,同时要根据网络的规模,保证磁盘上有充足的剩余空间.由于活动 ...

  9. windows下java开发资料汇总

    开发环境搭建:   (1) java开发环境配置    (2) maven环境快速搭建        项目部署:   (1) Eclipse中项目部署方法   (2) 使用Eclipse构建Maven ...

  10. 高斯混合模型Gaussian Mixture Model (GMM)

    混合高斯模型GMM是指对样本的概率密度分布进行估计,而估计采用的模型(训练模型)是几个高斯模型的加权和(具体是几个要在模型训练前建立好).每个高斯模型就代表了一个类(一个Cluster).对样本中的数 ...