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
题意:中序和后序建树,然后按zigzagging order输出。
分析:层序遍历的时候将节点输出到容器中,最后输出的时候根据奇数还是偶数来输出结点
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-28-14.24.50
* Description : A1127
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
;
int n,post[maxn],in[maxn];
vector<int> ans[maxn];
struct Node{
int val,layer;
Node* lchild;
Node* rchild;
Node(int _val,int _layer){
val=_val;
lchild=NULL;
rchild=NULL;
layer=_layer;
}
};
;
Node* create(int inL,int inR,int postL,int postR,int layer){
if(inL>inR) return NULL;
if(layer>maxlayer) maxlayer=layer;
int rootVal=post[postR];
Node* root=new Node(rootVal,layer);
int k;
for(int i=inL;i<=inR;i++){
if(post[postR]==in[i]){
k=i;
break;
}
}
int numLeft=k-inL;
root->lchild=create(inL,k-,postL,postL+numLeft-,layer+);
root->rchild=create(k+,inR,postL+numLeft,postR-,layer+);
return root;
}
void BFS(Node* root){
queue<Node*> q;
q.push(root);
while(!q.empty()){
Node* now=q.front();
q.pop();
ans[now->layer].push_back(now->val);
if(now->lchild!=NULL) q.push(now->lchild);
if(now->rchild!=NULL) q.push(now->rchild);
}
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
scanf("%d",&n);
;i<n;i++){
scanf("%d",&in[i]);
}
;i<n;i++){
scanf("%d",&post[i]);
}
Node* root=create(,n-,,n-,);
BFS(root);
;i<=maxlayer;i++){
){
printf(]);
continue;
}
==){
;j<ans[i].size();j++){
printf(" %d",ans[i][j]);
}
}
else{
;j>=;j--){
printf(" %d",ans[i][j]);
}
}
}
;
}
1127 ZigZagging on a Tree (30 分)的更多相关文章
- 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 (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]
题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...
- PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树
根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出.输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出 #include <iostream> ...
- 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
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
7-4 Cartesian Tree (30分) A Cartesian tree is a binary tree constructed from a sequence of distinct ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
随机推荐
- 反射,内省,BeanUtil的区别
PS:为了操作反射方便,sun创建了 内省, Apache闲麻烦自己创建了BeanUtils 1.开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所 ...
- 对spark算子aggregateByKey的理解
案例 aggregateByKey算子其实相当于是针对不同“key”数据做一个map+reduce规约的操作. 举一个简单的在生产环境中的一段代码 有一些整理好的日志字段,经过处理得到了RDD类型为( ...
- 初学者须知 常见的HTML5开发工具有哪些
HTML5被看做是Web前端开发的最佳编程语言,具有多设备.跨平台.即时更新等优势.更重要的是HTML5入门简单,就业前景广.薪资福利高,这促使越来越多的人转行学习HTML5.学习要一步一个脚印,今天 ...
- Gravitee.io Access Management docker-compose运行
Gravitee.io 官方提供的docker-compose 快速运行的方式 默认ui 账户 admin adminadmin 环境准备 docker-compose 文件 # # Copyrigh ...
- hermes kafka 转http rest api 的broker 工具
hermes 与nakadi 是类似的工具,但是设计模型有很大的差异,hermes 使用的是webhook的模式(push) nakadi 使用的是pull(event stream),各有自己解决的 ...
- nexus 私有 maven 仓库的搭建
下载地址 页面 : https://help.sonatype.com/repomanager3/download 首先需要安装jdk .安装棒法我 前面的文章有写.https://www.cnblo ...
- 2、visualBox虚拟机扩容
1.找到VBoxManager工具 1)打开Finder,找到[应用程序],在右侧找到VirtualBox.app,然后打开右键,找到[显示包内容],点击打开 2.打开终端,来到这个目录下 cd /A ...
- 大数据离线分析平台 JavaSDK数据收集引擎编写
JavaSDK设计规则 JavaSDK提供两个事件触发方法,分别为onChargeSuccess和onChargeRefund.我们在java sdk中通过一个单独的线程来发送线程数据,这样可以减少对 ...
- Visual Studio: 一键卸载所有组件工具,彻底卸载干净。
第一步.手动卸载VS主体 第二步.下载工具并解压 网盘下载地址:https://pan.baidu.com/s/1eSHRYxW 也可以在Github上下载最新版本:https://github.co ...
- ionic platform add ios, Error:spawn EACCES
RT: cordova ionic 环境搭建好之后,需要添加平台才能打包,添加平台如果出错:Error:spawn EACCES, 原因是因为没添加hooks, 请使用 ionic add hooks ...