1127. ZigZagging on a Tree (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

题意:通过后序遍历中序遍历复原二叉树,再按照题目要求的顺序输出,输出要求只要对层序遍历的方式稍加改动即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30000+5
typedef long long ll;
struct Node {
int key=INF, L, R;
Node() {}
Node(int key,int l,int r):key(key),L(l),R(r) {}
}node[N_MAX]; vector<int> in, post;int n, cnt;
void dfs(int n,int l,int r) {
if (l>r) {
node[n].key = INF;
return;
}
int root = post[cnt--];
node[n] = Node(root, * n+, * n + );
int k = find(in.begin(),in.end(),root)-in.begin();
dfs( * n + , k + , r);
dfs( * n+, l, k - );
}
int order[N_MAX];
vector<int>res; vector<int>level;
void bfs(int root) {
queue<int>que;
que.push(root);
order[root] = ;
while (!que.empty()) {
int p = que.front(); que.pop();
if (node[p].key != INF) {
res.push_back(node[p].key);
level.push_back(order[p]);
if (node[p].L != ) { order[node[p].L] = order[p] + ;que.push(node[p].L); }
if (node[p].R != ) { order[node[p].R] = order[p] + ;que.push(node[p].R); }
}
}
} int main() {
while (scanf("%d",&n)!=EOF) {
in.resize(n); post.resize(n);
for (int i = ; i < n; i++)scanf("%d",&in[i]);
for (int i = ; i < n; i++)scanf("%d", &post[i]);
cnt = n - ;
dfs(, , n - );
bfs();
int num = ,orde=;//num是每一层的计数器
vector<int>out;
for (int i = ; i < res.size();) {
out.clear();
while (i<res.size()&&orde ==level[i]) {
out.push_back(res[i]);
i++;
}
if (orde & ) {
for (int j = ; j < out.size(); j++)printf("%d%s", out[j], (i == res.size()&&j+==out.size())? "\n" : " ");
}
else {
for (int j = out.size() - ; j >= ; j--)printf("%d%s", out[j], (i == res.size()&&j== )? "\n" : " ");
}
orde++;
} }
}

pat 甲级 1127. ZigZagging on a Tree (30)的更多相关文章

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

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

  2. PAT甲级1127. ZigZagging on a Tree

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

  3. PAT 甲级 1127 ZigZagging on a Tree

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

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

  5. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

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

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

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

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

随机推荐

  1. SummerVocation_Learning--java的String类运用

    题目: 编写一个程序,输出一个字符串中的大写字母数,小写字母数,及其它字母数. 思路1: 可以先遍历整个字符串,在判断每个字符的类型. public class TestString { public ...

  2. vitrual box安装centos时一直黑屏的解决办法

    趁着清明节没事,昨天看了mysql性能优化后,想装个linux系统学习下,linux一直是我的短板...之前是在vmware上安装ubuntu,买了新电脑后,听过virtual box相比vmware ...

  3. linux配置邮箱服务

    配置邮箱服务Linux常见的邮箱客户端是mail或mutt:服务端有sendmail服务(centos 5).postfix服务(centos 6).这里我们不使用本地的邮件服务,而是使用本地的邮件客 ...

  4. JZOJ 5777. 【NOIP2008模拟】小x玩游戏

    5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms  Memory Limits ...

  5. Python9-From-CSS-day48

    1.form表单相关内容前后端有数据交互的时候用form表单form表单提交数据的几个注意事项: 1.所有获取用户输入的标签都必须放在form表单里面 2.action 控制着往哪里提交 3.inpu ...

  6. 离线安装 Visual Studio Express 而不下载整个镜像文件的方法(转载)

    转 visual studio 2010 express 全序列号 phone开发工具YDK44-2WW9W-QV7PM-8P8G8-FTYDF VC# 2010 Express: PQT8W-68Y ...

  7. 【转】Python操作MongoDB

    Python 操作 MongoDB   请给作者点赞--> 原文链接 这篇文章主要介绍了使用Python脚本操作MongoDB的教程,MongoDB作为非关系型数据库得到了很大的宣传力度,而市面 ...

  8. Redis实现之服务器

    命令请求的执行过程 一个命令请求从发送到获得回复的过程中,客户端和服务器需要完成一系列操作.举个栗子,如果我们使用客户端执行以下命令: 127.0.0.1:6379> SET KEY VALUE ...

  9. day40--mysql step4 SQLAlchemy

    1.unique = True 表示启动唯一索 2.有add 必须有commit这样数据才会提交 3.ORM功能 #!/usr/bin/env python # -*- coding:utf-8 -* ...

  10. NOI p 2017 TG游记

    嗨小朋友们大家好 还记得我是谁吗 对了我就是为iot配音的演员 弹鸡鸡 今天呐我特别的要向长沙市的oier们 洛谷的oier们 还有cnblogs的oier们问声好 为什么呢 因为我们在2017年11 ...