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 ...
随机推荐
- DevExpress06、Popup Menus、RadialMenu、XtraTabControl、SplitContainerControl、GroupControl
Popup Menus 弹出菜单 使用弹出菜单(popup menus),我们可以在 控件上 显示 上下文选项 或 命令. 弹出菜单是一个显示了特定项的窗体,用户可以选择这些项以执行 ...
- BBS论坛博客系统
目录 BBS网站需求分析 BBS数据库设计 BBS用户登录 BBS用户注册 BBS网站首页 BBS个人首页 后台管理系统搭建 网站全部源码
- Android友盟推送
当前版本号:v3.0.5 1.下载SDK解压并导入(import module,compile project(':PushSDK')),里面有demo,用demo的包名去官网添加一个应用,然后替换d ...
- 【转】Poco 1.4.2 HTTPClientSession/HTTPRequest 使用使用代理(proxy)需要注意的一点
Poco 1.4.2 HTTPClientSession/HTTPClientSession 在使用代理的时候,request的URI不能包含协议和主机.否则会出错. 不使用代理的时候,以下代码能正常 ...
- google浏览器window.onbeforeunload方法兼容问题
window.onbeforeunload方法在IE内核浏览器是有效的,但是在google浏览器中并不兼容,请教给位怎么在google浏览器中兼容window.onbeforeunload方法 采纳的 ...
- Odoo开发调试技巧
转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9307490.html 一:Odoo控制台error日志一般为以下格式 Traceback (most rec ...
- 关于 Spring AOP (AspectJ) 该知晓的一切
关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...
- WorldWind源码剖析系列:二维点类Point2d和三维点类Point3d
PluginSDK中的点主要有二维和三维两种类型,分别用来描述平面和立体点.其类图如下. 这两个类比较简单.其字段成员主要用来描述点对象在各坐标轴上的分量. 属性Length用来返回二维和三维点的距离 ...
- GitHub上最火的74个Android开源项目(三)
此前,推出的GitHub平台上“最受欢迎的开源项目”系列文章引发了许多读者的热议,在“GitHub上最火的40个Android开源项目(一).(二)中,我们也相继盘点了40个GitHub上最受欢迎的A ...
- 学习笔记---gson
1.gson变量定义注解 @SerializedName("char") private String charm;//mongo转化char 2.gson中json文件转化成类 ...