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的更多相关文章

  1. 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 ...

  2. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  3. PAT甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. pat 甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  5. 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 ...

  6. PAT 甲级 1127 ZigZagging on a Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...

  7. 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 ...

  8. PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树

    根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出.输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出 #include <iostream> ...

  9. 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 ...

随机推荐

  1. 深入理解JMM(Java内存模型) --(六)final

    与前面介绍的锁和volatile相比较,对final域的读和写更像是普通的变量访问.对于final域,编译器和处理器要遵守两个重排序规则: 在构造函数内对一个final域的写入,与随后把这个被构造对象 ...

  2. [转]C语言字符串操作总结大全(超详细)

    1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat(p, p1, n) 附加指定长度 ...

  3. Java 8 (7) 重构、测试和调试

    为改善可读性和灵活性重构代码 看到这里我们已经可以使用lambda和stream API来使代码更简洁,用在新项目上.但大多数并不是全新的项目,而是对现有代码的重构,让它变的更简洁可读,更灵活. 改善 ...

  4. 解决TeamViewer的“现在无法捕捉画面。可能由于快速切换或者远程桌面断开或者最小化”问题

    公司项目接近尾声,因为测试的需要,就在客户局域网内部搭建了几台虚拟机做为服务器给客户测试用. 虚拟机环境:win2008R2 原先在客户那边用远程登录虚拟机操作,但考虑到回公司后无法远程登录,所以安装 ...

  5. Angular——单页面实例

    基本介绍 1.引入的route模块可以对路由的变化做出响应 2.创建的控制器中依然需要$http向后台请求数据 3.php中二维数据的遍历用的是foreach 4.php中$arr=array(),$ ...

  6. 创建密码带有特殊字符的dblink

    使用的是data studio,所以末尾不加分号 create database link link_to_143 connect " using '(DESCRIPTION = (ADDR ...

  7. MyEclipse 快捷键大全(@Hcy)

    MyEclipse 快捷键1(CTRL)-------------------------------------Ctrl+1 快速修复Ctrl+D: 删除当前行 Ctrl+Q  定位到最后编辑的地方 ...

  8. 十年后我不会log,还是活的很好啊

    混迹于互联网也一两年了,出于喜爱与生活压力依然会从事很久.迟迟不写博客,大概是觉得知识与经验积累在笔记上时不时看看就好了,而实际情况是笔记很少翻,遇到问题搜博客和百度依然是首选,故开通博客记录自己工作 ...

  9. 怎么忽略ESLint校验

    方法一: 打开eslint的配置文件.eslintrc.js rules: { // allow async-await 'generator-star-spacing': 'off', // all ...

  10. 【nginx】解决nginx搭建图片服务器访问图片404

    图片通过ftp服务上传到/home/ftpuser/www/images目录下后访问 http://192.168.128.128/images/xxx.jpg 还是 404 NOT FOUND ,解 ...