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 ...
随机推荐
- bzoj4619
4619: [Wf2016]Swap Space Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 129 Solved: 54[Submit][Sta ...
- Java源码——Integer
最近在研究java的源代码,但是由于自己英语水平有限,所以想使用中文注释的方式把源码里的方法全部重写 一遍,下面是楼主整理出来的一小部分.我把整体的项目托管到GitHub上了,欢迎大家前去交流学习. ...
- PCB Genesis加邮票孔(弧与弧)实现算法
一.Genesis加邮票孔(弧与弧)实现算法 1.鼠标点击位置P点(可以确认搜索区域位置,确认点击位置周边元素分区,此所讲算法未应用到P点坐标) 2.求出:P1C与P2C (线与弧最近点距离的2个点) ...
- bzoj 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典【dp】
预处理出g[i][j]表示原串第i个匹配第j个单词需要去掉几个字母(匹配不上为-1) 设f[i]为i及之后满足条件要去掉的最少字母 倒着dp! f[i]初始为f[i+1]+1,转移方程为f[i]=mi ...
- 乐搏讲自动化测试-Python语言常识及前景(3)
相信小伙伴们都知道,随着软件测试行业的发展和进步自动化测试已经成为必然.在竞争日益激烈的市场环境中也是你升职加薪的利器. 所以,小编决定从今天起!将要系统.连续.高质量的持续更新「整套自动化测试」文章 ...
- ASP.NET MVC5 之 客户端实现文件的下载
MVC 实现下载功能主要借助于 File 属性: //下载文件接口 public ActionResult GetTrackTempIsc(ICSModels icsModels) { bool fl ...
- 【杂谈】HTML5到底给了我们什么?迟到的2016年终总结
其实提笔的时候日期已经到了3月了,不过由于在过去的2016年笔者发生了蛮多的事情,所以还是决定记录一下,那些关于成长的片段. 其实HTML5是在2012年的时候接触的,当时和结果志趣相投的同事,看到了 ...
- [C和指针] 6-指针
6.1 内存和地址 我们可以把计算机的内存看作是一条长街上的一排房屋,每座房子都可以容纳数据,并通过一个房号来标识. 这个比喻颇为有用,但也存在局限性.计算机的内存由以亿万计的位(bit)组成,每个位 ...
- Manacher BestCoder Round #49 ($) 1002 Three Palindromes
题目传送门 /* Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间 ...
- Rabin_Karp(hash) HDOJ 1711 Number Sequence
题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...