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. 【转载】C#调用C++ DLL

    using System; using System.Collections.Generic; using System.Linq; using System.Text; //1. 打开项目“Tzb” ...

  2. EPON ONU软件升级的若干优化方案

    1 说明 目前EPON ONU软件升级主要有IP方式(如SNMP/TR069)和TFTP+OAM两种.前者需占用大量IP地址,且配置ONU的IP地址需要手工操作,给业务开通和系统维护带来较大不便:后者 ...

  3. win7 开机自启动控制

    直接用win+r运行 --- 输入 msconfig 去除“OneNote”开机自启动方法:取消勾选,点击 “应用” ,然后点击“确定” 即可

  4. Android.mk (1) 函数

    https://www.jianshu.com/p/46224d15fc5f 从函数说起 大家都习惯看从头,从构建目标讲起的,导致每篇文档熟的都是前面的部分.很多教程也都是想办法能够观其大略,从整体上 ...

  5. MyBatis学习之输入输出类型

    1.  传递pojo对象 Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称,其中,#{}:占位符号,好处防止sql注入,${}:sql拼接符号, 简要说明 ...

  6. LeetCode 50 Pow(x, n) (实现幂运算)

    题目链接:https://leetcode.com/problems/powx-n/?tab=Description   Problem:实现幂运算即 pow(x,n)   设形式为pow(x,n)  ...

  7. Protobuf的安装使用

    date: 2018-10-12  18:59:13 版权归属原作者,本位转自:https://www.cnblogs.com/autyinjing/p/6495103.html 1. 是什么? Go ...

  8. Java 制作证书(Windows 和Linux)

    一.Windows数字证书 1. 生成数字证书 1.1 进入[%JAVA_HOME%]路径下 cd D:\Program Files\Java\jdk1.8.0_131 1.2 生成证书.一些命令中红 ...

  9. git 提示:fatal: remote origin already exists. 错误解决

    今天git连接远程库的时候出现fatal: remote origin already exists. 这个错误 大概是之前连接过别的库吧 然后我们的解决办法就是删除之前的连接 1.删除git远程仓库 ...

  10. ICSharpCode.SharpZipLib.dll 压缩多文件

    网站:http://icsharpcode.github.io/SharpZipLib/ 引用:ICSharpCode.SharpZipLib.dll private string CompassZi ...