根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出。
输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出

#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)-中序、后序建树的更多相关文章

  1. PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水

    由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍 ...

  2. 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 ...

  3. PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789138.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. PAT甲题题解-1053. Path of Equal Weight (30)-dfs

    由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...

  5. PAT甲题题解-1034. Head of a Gang (30)-并查集

    给出n和k接下来n行,每行给出a,b,c,表示a和b之间的关系度,表明他们属于同一个帮派一个帮派由>2个人组成,且总关系度必须大于k.帮派的头目为帮派里关系度最高的人.(注意,这里关系度是看帮派 ...

  6. PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)

    就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...

  7. PAT甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  8. pat 甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. PAT甲题题解-1068. Find More Coins (30)-dp,01背包

    一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...

随机推荐

  1. 【Alpha 冲刺】 10/12

    今日任务总结 人员 今日原定任务 完成情况 遇到问题 贡献值 胡武成 完成app端api编写 未完成 Json格式出了点问题,修复中 孙浩楷 图片在线编辑器插件引入 未完成 耦合了,结果另外一个那边做 ...

  2. 【adb命令】在cmd窗口中使用adb install命令安装 中文名字apk报错的解决办法

    1.在cmd窗口中使用adb install命令安装中文名字apk报错,安装英文名字apk就正常,详细报错如下图: 2.查看adb版本号:adb version 3.怀疑是adb版本的原因,尝试换个最 ...

  3. sql 一个表的字段更新至另一个字段的方法

    update Lc_Taxs set TaxMember = convert(int,Lc_Taxs2.TaxNo)  from Lc_Taxs a,(select * from Lc_Taxs ) ...

  4. [Usaco2007 Jan]Balanced Lineup

    嘟嘟嘟 一道RMQ板子题,分别维护最大值和最小值,不解释. #include<cstdio> #include<iostream> #include<algorithm& ...

  5. python-webbrowser模块 浏览器操作

    python的webbrowser模块支持对浏览器进行一些操作,对于爬虫来说是比较基础的知识点 1.主要有以下三个方法: webbrowser.open(url, new=0, autoraise=T ...

  6. 有crontab中的脚本不执行,需要在脚本里面export各种环境变量

    [oracle@sta ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashrc ]; ...

  7. 【转】mysql增量备份恢复实战企业案例

    来源地址:http://seanlook.com/2014/12/05/mysql_incremental_backup_example/ 小量的数据库可以每天进行完整备份,因为这也用不了多少时间,但 ...

  8. WorldWind源码剖析系列:绘制参数类DrawArgs

    绘制参数类DrawArgs主要对绘制时需要的对象如:设备对象Microsoft.DirectX.Direct3D.Device.Microsoft.DirectX.Direct3D.Font字体对象. ...

  9. nginx配置收集

    同个服务,分别读取不同借口 location /xibao/service_api/ { if ($request_uri ~ ^/xibao/(.*)) { set $xibao_data_url ...

  10. vxlan 简单理解 vs calico 网络模型

    1.vxlan(virtual Extensible LAN)虚拟可扩展局域网,是一种overlay的网络技术,使用MAC in UDP的方法进 行封装,共50字节的封装报文头. 2.VTEP为虚拟机 ...