1127. ZigZagging on a Tree (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  2. 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 ...

  3. PAT甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. 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 ...

  5. pat 甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  6. PAT_A1127#ZigZagging on a Tree

    Source: PAT A1127 ZigZagging on a Tree (30 分) Description: Suppose that all the keys in a binary tre ...

  7. A1127. ZigZagging on a Tree

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  8. 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 ...

  9. PAT 甲级 1127 ZigZagging on a Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...

随机推荐

  1. SpriteBuilder中使用TrueType字体的一些障碍

    在实践中,有一些小的陷阱和障碍可能阻止你使用一般的TrueType字体. 第一个,必须要有一个有效的字体文件.在Finder中双击该.ttf文件,应该会打开Font Book app,显示一个象形符号 ...

  2. Android MTK平台最完备的开机动画修改教程

    修改手机的开机动画不是什么难事儿. 但修改一款很冷门的"山寨机",就不太好修改第一屏了. 手机是MTK的一款手机,虽然比较贵(价格超过三星Note3),但在我看来跟山寨机木有啥区别 ...

  3. linux下挂载U盘

    转:http://www.cnblogs.com/yeahgis/archive/2012/04/05/2432779.html linux下挂载U盘 一.Linux挂载U盘: 1.插入u盘到计算机, ...

  4. Android高级控件(二)——SurfaceView实现GIF动画架包,播放GIF动画,自己实现功能的初体现

    Android高级控件(二)--SurfaceView实现GIF动画架包,播放GIF动画,自己实现功能的初体现 写这个的原因呢,也是因为项目中用到了gif动画,虽然网上有很多的架包可以实现,不过我们还 ...

  5. Android模拟器启动不了解决办法

    问题描述:Windows2008中的MyEclipse项目在Windows2003中运行时无法启动模拟器. 解决要点:启动模拟器管理工具,在启动中设置属性中不勾选默认尺寸显示. 系统错误如下: [20 ...

  6. VS2010中使用Jquery调用Wcf服务读取数据库记录

    VS2010中使用Jquery调用Wcf服务读取数据库记录 开发环境:Window Servere 2008 +SQL SERVE 2008 R2+ IIS7 +VS2010+Jquery1.3.2 ...

  7. 【49】java内部类剖析

    什么是内部类: 定义在其他类(outer class)中的类被称作内部类.内部类可以有访问修饰服,甚至可以被标记为 abstract 或 final. 内部类与外部类实例有特殊的关系,这种关系允许内部 ...

  8. ubuntu14.04使用rails连接mysql数据库

    rails自带的sqlite3各方面都不错,但是免费版缺少一个致命功能:加密码!虽说第三方有编译好的二进制版的加密版,但咱先不折腾鸟;直接上mysql吧. ubuntu安装mysql非常简单,先不聊; ...

  9. jsJqGrid

    /*展开收起*/ $(function() { initGridTable(); }); function change() { var flag = $("#searchTitle&quo ...

  10. javascript数组特性

    数组是一段线性分配的内存, 它通过整数计算偏移并访问其中的元素. 数组是一种性能出色的数据结构. 1.数组字面量 数组字面量提供了一种非常方便地创建新数组的表示法. 多个用逗号分隔的值的表达式. 数组 ...