PAT1127:ZigZagging on a Tree
1127. ZigZagging on a Tree (30)
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

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 inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the zigzagging sequence of the tree in a line. 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:
8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1
Sample Output:
1 11 5 8 17 12 20 15 思路
类似Pat1029,根据中序遍历和后序遍历序列确定树,以层次遍历的形式存入vector<vector<int>>中,然后按每一层往返输出就行。 代码
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int> postorder(31);
vector<int> inorder(31);
vector<vector<int>> levels(31); void buildTree(const int pl,const int pr,const int il,const int ir,const int level)
{ if(pl > pr || il > ir)
return;
int root = postorder[pr],i = 0;
while( inorder[il + i ] != root) i++;
levels[level].push_back(root);
buildTree(pl,pl + i - 1,il,il + i - 1,level + 1);
buildTree(pl + i ,pr - 1,il + i + 1,ir,level + 1);
} void zigzag()
{
cout << levels[0][0];
bool zigzag = false;
for(int i = 1; i < levels.size() && !levels[i].empty();i++)
{
if(zigzag)
{
for(int j = levels[i].size() - 1;j >= 0;j--)
{
cout << " " << levels[i][j];
}
}
else
{
for(int j = 0;j < levels[i].size();j++)
{
cout << " " << levels[i][j];
}
}
zigzag = !zigzag;
}
} int main()
{
int N;
while(cin >> N)
{
for(int i = 0;i < N;i++)
cin >> inorder[i];
for(int i = 0;i < N;i++)
cin >> postorder[i];
buildTree(0,N - 1,0, N - 1,0);
zigzag();
}
}
PAT1127:ZigZagging on a Tree的更多相关文章
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- 1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- PAT甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 1127 ZigZagging on a Tree[难]
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- pat 甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT_A1127#ZigZagging on a Tree
Source: PAT A1127 ZigZagging on a Tree (30 分) Description: Suppose that all the keys in a binary tre ...
- A1127. ZigZagging on a Tree
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
- PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
- PAT 甲级 1127 ZigZagging on a Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...
随机推荐
- 通过CSS显示垂直文本
原文链接: CSS Vertical Text 原文日期: 2014年03月18日 翻译日期: 2014年3月22日 翻译人员: 铁锚 示例地址: http://davidwalsh.name/dem ...
- ffdshow 源代码分析 7: libavcodec视频解码器类(TvideoCodecLibavcodec)
===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...
- 【cocos 2d-x】VS2012+win7+cocos2d-x3.0beta2开发环境配置
本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder 微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.com ...
- Linux - 主机的细部权限规划:ACL 的使用
ACL 是 Access Control List 的缩写,主要的目的是在提供传统的 owner,group,others 的 read,write,execute 权限之外的细部权限配置.ACL 可 ...
- bash下如何使用bind[En]
You can determine the character sequence emitted by a key by pressing Ctrl-v at the command line, th ...
- ubunut在系统恢复模式下无法修改root密码的分析和解决
前些日子本猫的ubuntu 14.10貌似出了点问题,想修改下root密码,但是无奈原系统有错正常情况下无法修改啊,这是逼我重装的节奏吗? 在ubuntu开机后立即按住left_shift不放,调出g ...
- Java不走弯路教程(4.Client-Server模式(1)-Server)
4.Client-Server模式(1)-Server 在上一章中,我们完成了MyDataBase.java的编写,类似于一个简单的数据库功能,提供了用户验证,查询操作. 在本章中,我们将继续扩展这个 ...
- oo第二次博客作业
多线程协同与同步控制总结 第五次作业-多线程电梯 本次作业是我第一次接触多线程,建立了请求模拟器.调度器和电梯运行三种线程.请求模拟器负责在输入后识别有效请求:调度器在扫描有效请求后将新的请求加入请求 ...
- ethtool确定网卡对应的物理网口
在配置有多个网络接口的设备时我们会犯难,eth0.eth1.--到底是那个接口? 我使用的机器是CentOS系统,打开终端,输入ethtool –help 显示帮助信息,下面我就简要介绍一下最常用的两 ...
- Table对象代表一个HTML表格,在文档中<table>标签每出现一次,一个table对象就会被创建。
1.对象集合 cells[] 返回包含表格中所有单元格的一个数组 rows[] 返回包含表格中所有行的一个数组 tBodies[] 返回包含表格中所有tbody的一个数组(主包含ty和td) 2.对象 ...