A1127. ZigZagging on a Tree
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data;
int lev;
}node;
int post[], in[], N;
node* create(int inL, int inR, int postL, int postR, int level){
if(inL > inR){
return NULL;
}
node* root = new node;
root->data = post[postR];
root->lev = level;
int mid;
for(mid = inL; mid <= inR; mid++){
if(in[mid] == root->data)
break;
}
int len = mid - inL;
root->lchild = create(inL, mid - , postL, postL + len - , level + );
root->rchild = create(mid + , inR, postL + len, postR - , level + );
return root;
}
void levelOrder(node* root){
queue<node*> Q;
Q.push(root);
int nowLev = root->lev;
int reverTag = ;
int cnt = ;
vector<node*> vec;
while(Q.empty() == false){
node* temp = Q.front();
Q.pop();
if(temp->lev == nowLev){
vec.push_back(temp);
}else{
nowLev = temp->lev;
if(reverTag == ){
reverTag = ;
for(int i = vec.size() - ; i>= ; i--){
cnt++;
printf("%d ", vec[i]->data);
}
}else{
reverTag = ;
for(int i = ; i < vec.size(); i++){
cnt++;
printf("%d ", vec[i]->data);
}
}
vec.clear();
vec.push_back(temp);
}
if(temp->lchild != NULL)
Q.push(temp->lchild);
if(temp->rchild != NULL)
Q.push(temp->rchild);
}
if(reverTag == ){
for(int i = ; i < vec.size(); i++){
cnt++;
if(cnt == N)
printf("%d", vec[i]->data);
else printf("%d ", vec[i]->data);
}
}else{
for(int i = vec.size() - ; i >= ; i--){
cnt++;
if(cnt == N)
printf("%d", vec[i]->data);
else printf("%d ", vec[i]->data);
}
}
}
int main(){
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%d", &in[i]);
}
for(int i = ; i <= N; i++){
scanf("%d",&post[i]);
}
node* root = create(, N, , N, );
levelOrder(root);
cin >> N;
return ;
}
总结:
1、题意:中序后序建树,再用层序输出,输出时一行逆序一行正序。
2、可以在建树时顺便将层次信息也加入节点。在层序遍历时使用一个vector,在当前层数相同时,仅仅把本该访问的节点存入vector,当层数发生改变时,输出vector内所有元素(设置一个计数器,如果上次是正序输出,则本次逆序输出); 或者正常层序遍历,再多用一个栈+一个队列,当需要正序输出该层时,节点入队列,需要逆序则节点入栈,层数改变时输出栈中或队列中全部节点,并清空。
A1127. ZigZagging on a Tree的更多相关文章
- 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 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甲级——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_A1127#ZigZagging on a Tree
Source: PAT A1127 ZigZagging on a Tree (30 分) Description: Suppose that all the keys in a binary tre ...
- 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 ...
- PAT1127:ZigZagging on a Tree
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- 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 ...
随机推荐
- C# Note33: 总结C# 6.0/7.0 新特性
先注明,本文主体参考自:C# 6.0新特性 目前代码中使用了很多C#6.0的新特性,下面以Point类来做相关叙述: public class Point { public int X { get; ...
- linux audit审计(7)--读懂audit日志
让我们先来构造一条audit日志.在home目录下新建一个目录,然后配置一条audit规则,对这个目录的wrax,都记录审计日志: auditctl -w /home/audit_test -p wr ...
- Chromecast
Chromecast chrome://cast/#offers https://chromecast.com/help https://www.pcmag.com/feature/326584/24 ...
- python数据结构与算法第六天【栈与队列】
1.栈和队列的原理 栈:后进先出(LIFO),可以使用顺序表和链表实现 队列:先进先出(FIFO),可以使用顺序表和链表实现 2.栈的实现(使用顺序表实现) #!/usr/bin/env python ...
- SSM框架整合系列——第一步
环境: JDK8 idea2018.2 maven3.5 spring和springMVC是天然集成,所以只需要解决mybatis和spring的整合问题,重点整合mybatis和spring的两个东 ...
- 接触Struts2的ModelDriven<>接口
最近在学SSH框架,实战项目,用到了Struts2的ModelDriven<>接口,在这做一点记录 ModelDriven,意为模型驱动,意思是直接把实体类当成页面数据的收集对象 参考他人 ...
- No module named 'ConfigParser'
系统: CentOS-6.4-x86_64 Python : Python 3.4.5 和 Python 3.5.2 安装 MySQL-python ,结果出错: ImportError: No mo ...
- 【python练习题】程序12
#题目:判断101-200之间有多少个素数,并输出所有素数. #判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数. from math import ...
- 使用update可以防止并发问题(保证数据的准确性),如果使用select会产生并发问题 ; select * from xx for update 给查询开启事务,默认情况下是没有事物的
update可以锁住数据防止数据被更新且导致与查询出的数据有误差,如果响应条数为0.说明更新失败 则可以回滚事务;
- c++ 实现哈夫曼树中遇见的问题
为了提高效率求得 叶子 节点中权值最小的两个元素,我们需要使用堆数据结构,它可以以O(logn)的复杂度 取得n个元素中的最小元素.为了绕过堆的实现,我们可以使用标准模板库中相应的标准模板—优先队列. ...