PAT1127:ZigZagging on a Tree

时间:2022-08-23 19:13:28

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.

PAT1127:ZigZagging on a 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 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&period; 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&period; ZigZagging on a Tree &lpar;30&rpar;

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

  4. PAT 1127 ZigZagging on a Tree&lbrack;难&rsqb;

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  5. pat 甲级 1127&period; ZigZagging on a Tree &lpar;30&rpar;

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

  6. PAT&lowbar;A1127&num;ZigZagging on a Tree

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

  7. A1127&period; 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. 学习shell中遇到的一些基础问题总结

    今天入门Shell脚本编程:今天的目标是完成冒泡排序,遇到了很多问题,总结了一下: 我从c转到shell遇到了这样的一些情况: 1:首先这几个符号非常重要 $变量:把变量的值拿出来使用(所以赋值语句应 ...

  2. 09&lowbar;IO流

    1. IO(Input Output)流 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作数据分为两种: 字节流和字符流 流按类型分 ...

  3. Python&colon; Convert rst to html

    pip install sphinx rst2html.py in_file.rst out_file.html

  4. kbengine&lowbar;js&lowbar;plugins 在Cocos Creator中适配

    kbengine_js_plugins 改动(2017/7/6) 由于Cocos Creator使用严格模式的js,而原本的kbengine_js_plugins是非严格模式的,因此为了兼容和方 便C ...

  5. XLua热更新用法全流程总结(所有容易出问题的点)

    Xlua热更新流程总结 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创 ...

  6. 在IDEA中将项目部署到Tomcat的方法及两种模式的区别

    转自:https://www.jianshu.com/p/fb0ed26c35d5 1.添加tomcat服务器 点右上角编辑配置   编辑配置 点击左上角+选择tomcat服务器   添加tomcat ...

  7. c&plus;&plus; vector常见用法

    //输出尾巴的元素 cout<<vec.back(); //定义vector迭代器 vector<int>::iterator ite=vec.begin(); for(ite ...

  8. thinkphp5中如何使用 usort

    thinkphp5中如何使用 usort 一.总结 一句话总结:其实比较函数加上命名空间就好啦,不然找不到 比较函数加命名空间 数组做usort的第二个参数 usort($question_list, ...

  9. 学习devexpresschartControl控件

    devexpress官网chart:https://documentation.devexpress.com/WindowsForms/8117/Controls-and-Libraries/Char ...

  10. ASP&period;NET Web API 框架研究 Controller创建 HttpController 类型解析 选择 创建

    上一篇介绍了HttpController的一些细节,接下来说下HttpController 类型解析.选择和创建.生产HttpController实例的生产线如下图: 一.涉及的类及源码分析 涉及的类 ...