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 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 <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=;
int pos[maxn],in[maxn];
int n,m;
vector<int> res[];
struct node{
int data;
node* left,*right;
int lvl;
};
node* create(int inl,int inr,int posl,int posr){
if(inl > inr) return NULL;
node* root = new node;
root->data = pos[posr];
int k;
for(k=inl;k<=inr;k++){
if(pos[posr]==in[k]) break;
}
int numleft=k-inl;
root->left = create(inl,k-,posl,posl+numleft-);
root->right = create(k+,inr,posl+numleft,posr-);
return root;
}
void pr(node* root){
queue<node*> q;
root->lvl=;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();
res[now->lvl].push_back(now->data);
if(now->left!=NULL){
now->left->lvl=now->lvl+;
q.push(now->left);
}
if(now->right!=NULL){
now->right->lvl=now->lvl+;
q.push(now->right);
}
}
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&in[i]);
}
for(int i=;i<n;i++){
scanf("%d",&pos[i]);
}
node* root = create(,n-,,n-);
pr(root);
int cnt=;
for(int i=;i<;i++){
if(i%==){
for(int j=res[i].size()-;j>=;j--){
printf("%d",res[i][j]);
cnt++;
if(cnt<n)printf(" ");
}
}
else{
for(int j=;j<res[i].size();j++){
printf("%d",res[i][j]);
cnt++;
if(cnt<n)printf(" ");
}
}
if(cnt==n)break;
}
}
注意点:最简单的一道30分题了。只要会建树,再层序遍历,把每层的数据保存起来,再根据层数正着输出或是反向输出就ac了。
ps:也可以用deque双向队列更方便的实现。每层的输出其实可以不用开vector数组,一层遍历完后输出就好了。
PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历的更多相关文章
- 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 ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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 ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
7-4 Cartesian Tree (30分) A Cartesian tree is a binary tree constructed from a sequence of distinct ...
- Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
随机推荐
- mysql 8小时超时设置
1.打开MySQL配置文件 2.添加 interactive_timeout=31536000wait_timeout=31536000 3.重新启动服务 打开MySQL命令行界面查看设置是否成功
- 后端自测必备神器-PostMan
作为后端的一个小小菜鸟,写代码没有把握,总怕出错,也不敢直接扔测试,这个时候就需要一个神器能够辅助自己去测试各种情况,让自己安心的交给测试,嗯……这时神器出场了------PostMan.在一个偶然的 ...
- groovy使用范型的坑
java的范型 Map<String, Integer> map = new HashMap<>(); map.put("a", 100); map.put ...
- Python3 系列之 编程规范篇
编码规范 编码 如无特殊情况, 文件一律使用 UTF-8 编码 如无特殊情况, 文件头部必须加入 #--coding:utf-8-- 标识 缩进 统一使用 4 个空格进行缩进 引号 自然语言 使用双引 ...
- MVC中Controller与View中间的数据传递的常用方法
这几天正在学习MVC,顺便就将自己每天的学习心得记录下来与大家分享一下吧! 在MVC中,Controller与View之间传递数据是很频繁的事情,所以在这里就总结一下我自己在学习中使用的几种常用的方法 ...
- canvas-0trasform.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [简记] github 上的 GraphQL v4 API
突发奇想,想用github做一个支持rss的blog体系,或者就是知识管理体系,简单看了下,把测试用的暂存起来 # Type queries into this side of the screen, ...
- java源文件与类
一个源文件可以包含多个类, 编译的时候,每一个类生成一个字符码文件, 源文件名可以和类名不一致,但字符码文件与类名一致, 如果类是public(公共类),源文件名必须与类名一致 命名规则:源文件的路径 ...
- FreeSSHD login with permission denied
登录遇到问题: Permission denied, please try again. 解决方法: 在window中使用freesshd开启ssh后,客户端登陆时报 access denied错误 ...
- 新浪微博POI点签到数据及可视化的初步成果
目前仅对山东省区域进行了抓取,权限不够高,抓取的速度非常慢,所以导致效率比较低... 数据抓取采用调用微博开放平台API的方法,数据存储采用mysql,格点数据分辨率为30″,山东省的MBR范围内(包 ...