PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树
根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出。
输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <string>
#include <map>
#define LEFT 0
#define RIGHT 1
using namespace std;
const int maxn=;
int inorder[maxn];
int postorder[maxn];
int level[maxn][maxn]; //每层的节点id
int levelcnt[maxn]; //每层的节点个数
int maxlayer=;
int cnt=; //节点id
struct Node{
int left=-,right=-;
int val;
}node[maxn]; //根据中序遍历和后序遍历建立树
void build(int inL,int inR,int postL,int postR,int fa,int LorR){
if(inL>inR)
return;
int val=postorder[postR];
int idx;
//在中序遍历中找出父亲节点的索引,其左边是左子树,右边是右子树
for(int i=inL;i<=inR;i++){
if(inorder[i]==val){
idx=i;
break;
}
}
int lnum=idx-inL;
cnt++;
node[cnt].val=val;
if(LorR==LEFT)
node[fa].left=cnt;
else if(LorR==RIGHT)
node[fa].right=cnt;
int tmp=cnt;
build(inL,idx-,postL,postL+lnum-,tmp,LEFT);
build(idx+,inR,postL+lnum,postR-,tmp,RIGHT);
}
void dfs(int root,int layer){
if(root==-)
return;
maxlayer=max(layer,maxlayer);
level[layer][levelcnt[layer]]=root;
levelcnt[layer]++;
dfs(node[root].left,layer+);
dfs(node[root].right,layer+);
}
int main()
{
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>inorder[i];
for(int i=;i<=n;i++)
cin>>postorder[i];
build(,n,,n,-,-);
dfs(,);
bool flag=true;
printf("%d",node[].val);
for(int i=;i<=maxlayer;i++){
if(flag){
for(int j=;j<levelcnt[i];j++)
printf(" %d",node[level[i][j]].val);
}
else{
for(int j=levelcnt[i]-;j>=;j--)
printf(" %d",node[level[i][j]].val);
}
flag=!flag;
}
return ;
}
PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树的更多相关文章
- PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水
由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍 ...
- 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 ...
- PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789138.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...
- PAT甲题题解-1034. Head of a Gang (30)-并查集
给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...
- PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...
- 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甲题题解-1068. Find More Coins (30)-dp,01背包
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...
随机推荐
- Jenkins 基于 Docker git JAVA CI/CD
准备两台机器 192.168.31.200 centos7 docker harbor git 192.168.31.201 centos7 docker jenkins maven git Ha ...
- 《面向对象程序设计》c++第六次作业___calculator SE
c++第五次作业 Calculator SE 代码 PS:这次作业延迟了很久,人要是迷茫啊----唉------ 新增GUI界面,使用Qt creator编写,纯代码生成控件.写坐标. 感觉Qt cr ...
- [python] 私有变量和私有方法
1.在Python中要想定义的方法或者变量只在类内部使用不被外部调用,可以在方法和变量前面加 两个 下划线 #-*- coding:utf-8 -*- class A(object): name = ...
- 网络编程_UDP协议_聊天程序
发送端:(将数据源改为键盘录入) import java.io.BufferedReader; import java.io.IOException; import java.io.InputStre ...
- 《Linux大棚命令百篇下》网络篇的总结
本文是<Linux大棚命令百篇下>网络篇的总结 ping -c 指定数量,在windows下会自动停止,linux下会一直ping下去 -q 简短报告 -s 指定每次ping的数据包大小, ...
- Grunt-Kmc基于KISSY项目打包
Grunt-Kmc基于KISSY项目打包 1. Grunt-Kmc 是基于nodejs开发的,所以未安装nodeJS,先需要安装nodejs.安装步骤如下: 1. 下载安装文件,下载地址 ...
- JAVA框架 Spring AOP注解
一.准备工作: 1)导入jar包: 4个jar包. 2)约束:(spring需要所有的约束)有IOC约束和AOP 还有事务(tx)以及注解注入的约束(context). <?xml versio ...
- js中数组的使用
使用js时候,很多情况下要使用数组.就写写数组中一些常用的方法. js中定义一个数组,一般有以下2种方法. 1. var arr = new Array(3); // 声明数组有3个元素 arr[0] ...
- CAN总线优势
CAN总线优势 RS-485基于R线构建的分布式控制系统而言, 基于CAN总线的分布式控制系统在以下方面具有明显的优越性: 首先,CAN控制器工作于多主方式,网络中 的各节点都可根据总线访问优先权(取 ...
- 快速在Ubuntu安装PHP网站
快速安装使用的是tasksel,tasksel是Debian / Ubuntu的工具,安装多个相关软件包作为一个协调的“任务”到你的系统.这个lamp-server^跟taskel这个程序有关.下面是 ...