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 ...
随机推荐
- fcntl获取和修改文件打开状态标志
[root@bogon code]# cat b.c #include<stdio.h> #include<error.h> #include<unistd.h> ...
- Android 工具视频学习笔记_WDS
1. 由于Android源码过于庞大,SourceInsight会经常卡死,不适合了.适合的是Android Studio, 非常好用.使用手册上有介绍如何安装. 编译安卓的过程说明手册中也有. 3. ...
- 05C++引用
1.变量名 变量名实质上是一段连续存储空间的别名,是一个标号(门牌号): 通过变量来申请并命名内存空间: 通过变量的名字可以使用内存空间. 2.引用的概念 变量名,本身是一段内存的引用,即别名(ali ...
- Autofac学习之三种生命周期:InstancePerLifetimeScope、SingleInstance、InstancePerDependency 【转载】
InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象:每次都用同一个对象: In ...
- chrome浏览器好用的插件
1.Chrome批量保存所有选项卡网址 + 批量打开复制网址小插件 批量保存所有选项卡网址插件:Copy All Urls 经常搜索一些东西,下班时无法处理完所有网页内容,比如做笔记什么的,又不舍得关 ...
- 上外网tunnel手段
需要的软件 1, httptunnel软件,包括服务端和客户端,家里开启服务端,公司开启客户端 2,(可选)proxifier PE,用来在公司check 代理工作是否正常 3,动态域名服务,在家里用 ...
- [转]总结@Autowired 和@Resource
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了. @Resource有两个属性是比较重要的,分 ...
- oracle之 反向键索引
反向键索引是一种B-tree索引,它在保持列顺序的同时,物理地改变每个索引键的字节(反向键索引除了ROWID和still之外,反转每个索引列的字节).例如,如果索引键为20,如果在十六进制中存储为这个 ...
- Jmeter --- 组件执行顺序与作用域
一.Jmeter重要组件: 1)配置元件---Config Element: 用于初始化默认值和变量,以便后续采样器使用.配置元件大其作用域的初始阶段处理,配置元件仅对其所在的测试树分支有效,如,在同 ...
- mysql的变量信息详解
mysql的变量详解 执行show variables命令可以查看MySQL服务器的变量 变量名 默认值 说明 对应的配置文件参数 auto_increment_increment 1 自增长类型的初 ...