pat甲级1020中序后序求层序
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
已知后序中序求先序:
void pre(int root, int begin, int end)
{
if (begin > end) return;
cout << post[root] << " ";
int p;
for (p = begin; p <= end; p++)
{
if (in[p] == post[root])
break;
}
pre(root - end + p - , begin, p - );
pre(root - , p + , end);
}
后序中最后一个元素为跟,找出跟在中序中的位置就可以确定左右子树的节点个数,然后就可以递归的去求解。
这个题是求层序遍历,开始使用的构造树然后再广度优先搜索的方法,比较繁琐。后来看柳婼的博客发现使用数组的方法构造树比较方便。
若当前根的下标为i,则左节点下标为 2 * i + 1,右节点下标为 2 * i + 2。
#include <iostream>
#include <vector>
#include <math.h>
using namespace std; vector<int> post, in, level(, -);
void getLevel(int root, int begin, int end, int index); int main()
{
int N, i;
cin >> N;
post.resize(N);
in.resize(N);
for (i = ; i < N; i++)
cin >> post[i];
for (i = ; i < N; i++)
cin >> in[i];
getLevel(N - , , N - , );
int cnt = ;
for (int i : level)
{
if (i != -)
{
if (cnt > )
cout << " ";
cnt++;
cout << i;
if (cnt == N)
break;
}
}
return ;
} void getLevel(int root, int begin, int end, int index)
{
if (begin > end) return;
level[index] = post[root];
int p;
for (p = begin; p <= end; p++)
{
if (in[p] == post[root])
break;
}
getLevel(root - end + p - , begin, p - , * index + );
getLevel(root - , p + , end, * index + );
}
pat甲级1020中序后序求层序的更多相关文章
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树: SDUT 1489 Description 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input 输入数据有多组,第一行是一个整数t (t& ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- 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 Level) 1124~1127:1124模拟 1125优先队列 1126欧拉通路 1127中序后序求Z字形层序遍历
1124 Raffle for Weibo Followers(20 分) 题意:微博抽奖,有M个人,标号为1~M.从第S个人开始,每N个人可以获奖,但是已获奖的人不能重复获奖,需要跳过该人把机会留给 ...
随机推荐
- shell脚本知识点汇总
sed中在对内容进行修改时,有时候需要引用外部变量的值或者获取一个shell命令执行的结果,以便达到更加可观的输出结果 1.sed中使用变量替换1)sed命令使用双引号的情况下,使用$var直接引用[ ...
- yarn快速使用及实践建议
什么是 yarn? 简单来说,yarn 是一个与 npm 功能相同的工具,用于前端项目的依赖管理.在使用 npm 的项目中,使用 npm 命令的地方都可以使用 yran 来代替. 为什么要使用 yar ...
- flask-sqlalchemy中 backref lazy的参数实例解释和选择
官方文档:http://docs.sqlalchemy.org/en/rel_1_0/orm/basic_relationships.html#relationship-patterns 最近在学习到 ...
- 整合spring和hibernate框架
一)整合spring和hibernate框架整合要点:(1)数据源配置在Spring的配置文件中,供Spring和Hibernate框架共同使用:(2)不再需要hibernate.hbm.xml配置文 ...
- [WebShow系列] Web浏览器最大化满屏及比例缩放方法
如果要在大屏上展示,大屏所带电脑的浏览器应该处于满屏,此时就不会显示浏览器软件的边框了.个别浏览器在满屏状态下,某些边栏等还继续保留,此时应设置此浏览器的显示选项方可消除. 如果屏幕中的显示对象过小或 ...
- CF10D/POJ2127 LCIS解题报告
题目传送门(洛谷)(CF)(POJ) 前言 期末考试前的最后一篇题解,希望期末考 rp++ 奇怪,为什么在CF上能过的代码到POJ上就 听取WA声一片 (不管了) 题目思路 LCIS模版O(n²) ...
- little w and Soda(思维题)
链接:https://ac.nowcoder.com/acm/contest/297/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- BeanFilterUtil
package com.yundaex.utility.bean.filter; import java.util.ArrayList; import java.util.List; import o ...
- Unity3D游戏高性能战争迷雾系统实现
一 效果图 先上效果图吧,这是为了吸引到你们的ヽ(。◕‿◕。)ノ゚ 战争迷雾效果演示图 战争迷雾调试界面演示图 由于是gif录制,为了压缩图片,帧率有点低,实际运行时,参数调整好是不会像这样一卡一顿的 ...
- vim配置成c++IDE
mv ~/.vimrc ~/.vimrcbak mv ~/.vim ~/.vimbak git clone https://github.com/handy1989/vim.git mv vim/.v ...