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个人可以获奖,但是已获奖的人不能重复获奖,需要跳过该人把机会留给 ...
随机推荐
- ASP.NET MVC 小牛之旅4:ASP.NET MVC的运行生命周期
ASP.NET MVC的运行生命周期大致分成三大过程:(1)网址路由对比. (2)运行Controller与Action. (3)运行View并回传结果. 4.1网址路由对比 当iis收到http请求 ...
- go培训课程都学什么?xorm框架学习系列(二):xorm结构体映射规则和表操作
上节内容我们学习了基本的xorm框架的知识和基础配置的相关信息.本节课内容我们继续学习相关的知识和相关操作. 名称映射规则 名称映射规则主要负责结构体名称到表名和结构体field到表字段的名称映射. ...
- 《OD学hadoop》20160903某旅游网项目实战
一.大数据的落地点 1.数据出售 数据商城:以卖数据为公司的核心业务 2. 数据分析 百度统计 友盟 GA IBM analysis 3.搜索引擎 4. 推荐系统 mahout 百分比 5.精准营销 ...
- luogu3224 永无乡(动态开点,权值线段树合并)
luogu3224 永无乡(动态开点,权值线段树合并) 永无乡包含 n 座岛,编号从 1 到 n ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 n 座岛排名,名次用 1 到 n 来表示.某些 ...
- OC 中的 weak 属性是怎么实现的?
OC 中的 weak 属性是怎么实现的,为什么在对象释放后会自动变成 nil?本文对这个问题进行了一点探讨.环境 mac OS Sierra 10.12.4 objc709参考答案 搜索后发现runt ...
- 防止表单submit或按钮button多次连续点击提交
如上例子:当我点击提交按钮触发submitQuartz()函数 防止用户连续点击提交操作 方法一:获取当时点击时间,根据时间差判断 $scope.submitQuartz=function () { ...
- shell学习(11)- seq
今天是五一劳动节,窗户外边,草长莺飞,惠风和畅,但坐在办公室里值班也需要做点事情,今天就写写seq的用法. 作用:用于以指定增量从首数开始打印数字到尾数,即产生从某个数到另外一个数之间的所有整数,并且 ...
- web安全深度剖析pdf
Web安全深度剖析.pdf_免费高速下载|百度网盘-分享无限制 链接:https://pan.baidu.com/s/1kVwP7SF
- EIGRP-1-EIGRP的基础和演变
值得一提的是,在2013年,Cisco决定开放EIGRP的定义,并将其发布为IETFInternet草案,即RFC的前身:文档名称为draft-savage-eigrp.从此,基本的EIGRP不再是机 ...
- Extending JMeter – Creating Custom Config Element – Property File Reader
JMeter is one of the best open source tools in the Test Automation Community. It comes with all the ...