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 ...
随机推荐
- AspectJ用注解替换xml配置
AspectJ基于注解的使用 AspectJ简介 AspectJ是一个基于Java语言的AOP框架,一般 其主要用途:自定义开发 一般情况下spring自动生成代理,要配置aop, 首先确定目标类,a ...
- [转]Java 的强引用、弱引用、软引用、虚引用
1.强引用(StrongReference) 强引用是使用最普遍的引用.如果一个对象具有强引用,那垃圾回收器绝不会回收它.如下: Object o=new Object(); // 强引用 当内存空间 ...
- SQL 添加索引
使用CREATE 语句创建索引 CREATE INDEX index_name ON table_name(column_name,column_name) include(score) 普通索引 C ...
- 包装类接受string 会自动将数字类型string转换成对应得包装类型
- 洛谷 P1102 A−B数对
题目描述 出题是一件痛苦的事情! 题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的 A+BA+BA+B ProblemProblemProblem ,改用 A−BA-BA−B 了哈哈! 好吧,题目是这 ...
- redis哨兵集群+spring boot 2.×
Ubuntu集群构建篇 redis-cli:不跟参数,默认访问localhost:6379端口,无密码登陆 redis-cli -h ${host} -p ${port} -a ${password} ...
- Android中HttpURLConnection对象是怎么生成的
try { URL mUrl = new URL("https://www.jianshu.com/"); HttpURLConnection http = (HttpURLCon ...
- Newtonsoft.Json 概述
有时候,在前后台数据交互或者APP与后台交互的时候,我们通常会使用Json进行数据交互,为此会使用到Newtonsoft.Json.dll 这个类库,这个类库非微软官方,但是下载量已经超过了数十万次, ...
- BZOJ 1912 巡逻(算竞进阶习题)
树的直径 这题如果k=1很简单,就是在树的最长链上加个环,这样就最大化的减少重复的路程 但是k=2的时候需要考虑两个环的重叠部分,如果没有重叠部分,则和k=1的情况是一样的,但是假如有重叠部分,我们可 ...
- 南理第八届校赛同步赛-C count_prime//容斥原理
大致思路就是先求出n的质因数假设是a1-an,然后在1-a的区间里面查找至少能整除{a1,a2...an}中一个元素的数有多少个,对1-b也做相同的处理,而找出来的元素肯定是与n不互质的,那么把区间的 ...