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 ...
随机推荐
- Beta版本项目计划
小队名称:PHILOSOPHER 小组成员 [组长]金盛昌(201421122043).刘文钊(20142112255).陈笑林(201421122042) 张俊逸(201421122044).陈志建 ...
- 10.Solr4.10.3数据导入(DIH全量增量同步Mysql数据)
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.创建MySQL数据 create database solr; use solr; DROP TABLE ...
- 利用gulp 插件gulp.spritesmith 完成小图合成精灵图,并自动输出样式文件
安装依赖 yarn add gulp yarn add gulp.spritesmith 本次依赖的版本是: "gulp": "^3.9.1" "gu ...
- Android友盟推送
当前版本号:v3.0.5 1.下载SDK解压并导入(import module,compile project(':PushSDK')),里面有demo,用demo的包名去官网添加一个应用,然后替换d ...
- Node.js实战(七)之交互式解释器
Node.js REPL(Read Eval Print Loop:交互式解释器) 表示一个电脑的环境,类似 Window 系统的终端或 Unix/Linux shell,我们可以在终端中输入命令,并 ...
- 一兄弟把/etc/init.d/functions误删除了,这是多么悲催的节奏啊;
RPM resource /lib/lsb/init-functions /lib/lsb/init-functions vs. /etc/init.d/functions in init scrip ...
- JS时间轴效果(类似于qq空间时间轴效果)
在上一家公司写了一个时间轴效果,今天整理了下,感觉有必要写一篇博客出来 给大家分享分享 当然代码还有很多不足的地方,希望大家多指点指点下,此效果类似于QQ空间或者人人网空间时间轴效果,当时也是为了需求 ...
- HDU 1811 Rank of Tetris(并查集+拓扑排序 非常经典)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Oracle 解决【ORA-01704:字符串文字太长】
错误提示:oracle在toad中执行一段sql语句时,出现错误‘ORA-01704:字符串文字太长’.如下图: 原因:一般为包含有对CLOB字段的数据操作.如果CLOB字段的内容非常大的时候,会导致 ...
- 添加默认的过滤条件xml
<search string="Search Sales Origin"> <field name="name"/> <field ...