PAT 1127 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<iostream>//根据中,后序建树,再层级遍历
#include<vector>
#include<queue>
using namespace std;
int n;
vector<int> post(30, 0), in(30, 0);
struct node{
int val;
node* left=NULL;
node* right=NULL;
node(int v):val(v), left(NULL), right(NULL){
}
};
node* buildtree(node* root, int inl, int inr, int postl, int postr){
if(inl>inr) return NULL;
root=new node(post[postr]);
int i=inl;
while(i<=inr&&in[i]!=post[postr]) i++;
int cnt=i-inl;
root->left=buildtree(root->left, inl, i-1, postl, postl+cnt-1);
root->right=buildtree(root->right, i+1, inr, postl+cnt, postr-1);
return root;
}
int main(){
cin>>n;
for(int i=0; i<n; i++)
cin>>in[i];
for(int i=0; i<n; i++)
cin>>post[i];
node* root=NULL;
root=buildtree(root, 0, n-1, 0, n-1);
queue<node*> q;
int flag=0, k=0, level=0;
q.push(root);
cout<<root->val;
while(!q.empty()){
vector<node*> v;
while(!q.empty()){
node *t=q.front();
q.pop();
if(t->left) v.push_back(t->left);
if(t->right) v.push_back(t->right);
}
for(int i = 0; i < v.size(); i++)
q.push(v[i]);
if(level % 2 == 0)
for(int i = 0; i < v.size(); i++)
printf(" %d", v[i]->val);
else
for(int i = v.size() - 1; i >= 0; i--)
printf(" %d", v[i]->val);
level ++;
}
return 0;
}
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 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- 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 ...
- 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 ...
- PAT 甲级 1127 ZigZagging on a Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...
- 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 A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
随机推荐
- SVN工具使用技巧
SVN打tag SVN打tag是一个很常用的功能,要谈打tag,还得从SVN官方推荐的目录结构说起.SVN官方推荐在一个版本库的根目录下先建立trunk.branches.tags这三个文件夹,其中t ...
- uva1563
https://vjudge.net/problem/UVA-1563 高斯消元解同余方程组 就是把原来的除法换成逆元,其他的都一样 #include<bits/stdc++.h> usi ...
- oracle创建数据库与配置监听器
上述是数据库创建完毕 *检测创建 -------------------------------------------------------------2配置监听器---------------- ...
- bzoj1052覆盖问题(二分+贪心)
1052: [HAOI2007]覆盖问题 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2446 Solved: 1131[Submit][Stat ...
- SP2916 GSS5 - Can you answer these queries V
给定一个序列.查询左端点在$[x_1, y_1]$之间,且右端点在$[x_2, y_2]$之间的最大子段和,数据保证$x_1\leq x_2,y_1\leq y_2$,但是不保证端点所在的区间不重合 ...
- [App Store Connect帮助]三、管理 App 和版本(7)移除 App
若要在“我的 App”主视图中移除 App,要先将此 App 从 App Store 中移除,并将所有与之关联的 App 内购买项目下架. 此外,仅当 App 的所有版本均处于下列状态之一时方可移除: ...
- GoAhead4.1.0 开发总结二(自定义使用)
环境 官方文档:https://www.embedthis.com/goahead/doc/ 源码下载: goahead-4.1.0-src.tgz 系统平台:Ubuntu 12.04.4 gcc v ...
- org.apache.poi.hssf.util.Region
从POI 3.18开始被Deprecated,在3.20版本中被移除了,所以3.20以前的都有 为了避免这个问题,用CellRangeAddress代替Region,其用法相同
- svg image 图片无法铺满 circle 的问题解决
引子 使用d3.js绘制了力布图后,需要在circle中绘制图片,方法如下: // 绘制图片 drawPattern(gContainer) { let that = this; let gPatte ...
- 二分查找 HDOJ 2141 Can you find it?
题目传送门 /* 题意:给出一个数,问是否有ai + bj + ck == x 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x */ ...