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; ...
随机推荐
- Docker常用命令(二)
Docker常用命令 查看Docker所有正在运行的容器 docker ps 查看Docker已退出的容器 docker ps -a 查看Docker所有镜像 docker images 删除镜像 删 ...
- 【Java并发编程】6、volatile关键字解析&内存模型&并发编程中三概念
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- addEventListener.js
document.addEventListener("click",function(){ console.log("添加事件监听") }) 举个例子 : 点击 ...
- onkeypress 在js函数返回false后没有反应
一.解决方案: 把 onkeypress = "function()" 改为 onkeypress = "event.returnValue=function()&quo ...
- VUE组件 之 Toast (Vue.extend 方式)
一.效果图 二.说明 这类提示框组件我们通常都会直接在 JS 代码中进行调用.像下面这样: this.$toast('别点啦,到头啦!') 但看到网上大多数还是通过 component 方式实现的, ...
- android recovery 升级时间与速度研究
从android4.4到现在android7.1升级,现在的升级包越来越大,一个system分区大小都分配了2G,整个升级包达到了接近500M, 升级包解压之后1G+,奇怪的是,发现了两个不同批次的板 ...
- JMeter 脚本开发(五)
一.JMeter 元件运行顺序 执行顺序逻辑如下: 1.配置元件(如果存在) 2.前置处理器(如果存在) 3.定时器(如果存在) 4.取样器(如果存在) 5.后置处理器(如果存在且取样器的结果不为空) ...
- Sql Server 判断字符串是否可以转数字
主要是在sql server的内置系统函数ISNUMERIC的基础上,将例外的“+”.“-”.“$”等也进行判断. CREATE FUNCTION [dbo].[fn_IsNumberic]( @st ...
- php二维数组去重
php二维数组去重 前言:php一维数组去重很简单,直接array_unique($arr)即可,但是二维数组去重就得自己去写了 二维数组去重方法: /* * 二维数组去重 * 注意:二维数组中的元素 ...
- Django【进阶篇】
目录 一.Model 二.admin 三.Form组件 四.Cookie 五.Session 六.分页 七.序列化 一.Model 数据库的配置 1.django默认支持sqlite,mysql, o ...