pat 甲级 1127. ZigZagging on a Tree (30)
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 题意:通过后序遍历中序遍历复原二叉树,再按照题目要求的顺序输出,输出要求只要对层序遍历的方式稍加改动即可。
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++;
} }
}
最新文章
- ubuntu 用apt-get 安装apache 和php 之后php不能解析的问题
- [New Portal]Windows Azure Virtual Machine (20) 关闭Azure Virtual Machine与VIP Address,Internal IP Address的关系(2)
- 20款精致的长阴影 LOGO 设计【附免费生成工具】
- Asp.net MVC验证那些事(1)-- 介绍和验证规则使用
- REDIS 在电商中的实际应用场景(转)
- storage size of ‘oldact’ isn’t known
- JS阻塞的问题
- 子查询解嵌套in改写为exists
- 在用VC编译下debug和release的什么区别
- 实现ARC文件与MRC文件互转,和混合使用。
- windows身份验证,那么sqlserver的连接字符串的
- altium designer14的Import wizard 为空的解决方法
- NSInteger到底是什么数据类型
- Scala关于软件的安装
- Python 使用图灵机器人实现微信聊天功能
- jsp页面执行java语法,获取的值在页面调用
- Web jsp开发学习——Servlet提交表单时用法
- python pandas 豆瓣电影 top250 数据分析
- Enterprise Library 4.1 参考源码索引
- Android四大组件之Intent(续2)
热门文章
- 问题009:java当中的关键字有哪些?在Editplus文本编辑软件中是什么颜色的?java当中的标识符有什么要求?Java中注释分为几类?
- 用css去除chrome、safari等webikt内核浏览器对控件默认样式
- python基础 字典排序
- 三十一、MySQL 及 SQL 注入
- Linux问题分析或解决_samba无法连接
- vmware 开机自动启动
- 第37课 thinkphp5添加商品基本信息及通过前置钩子上传商品主图 模型事件(勾子函数)
- oracle 事务 第二弹
- POJ1426-Find The Multiple(搜索)
- sql server 不可见字符处理 总结