1020. Tree Traversals (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020
and the source code is as followed.
#include<iostream>
#include<cstdlib>
#include<queue> using namespace std; const int maxx = ; typedef struct Tree
{
Tree *le;
Tree *ri;
int data;
}Tree; Tree *root; int pos[maxx],in[maxx]; void printLevelOrder(Tree *root)
{
queue<Tree*> que;
Tree *tr = NULL;
que.push(root);
bool flg = true;
while (!que.empty())
{
tr = (Tree *)que.front();
que.pop();
if (tr == NULL) continue;
if (flg)
{
printf("%d",tr->data);
flg = false;
}else
{
printf(" %d",tr->data);
}
que.push(tr->le);
que.push(tr->ri);
}
printf("\n");
} //构造树pl为后序序列的左边界pr为其右边界
//il为中续遍历的左边界ir为其右边界
Tree *buildTree(int pl,int pr,int il,int ir)
{
if (pl > pr)return NULL;
int p = il;
while (in[p] != pos[pr])
{
++p;
}
Tree *tree = (Tree *)malloc(sizeof(Tree));
tree->data = pos[pr];
tree->le = buildTree(pl,pr-ir+p-,il,p-);
tree->ri = buildTree(pr-ir+p,pr-,p+,ir); return tree;
} int main(){
int n,i;
Tree *root; scanf("%d",&n);
for(i=;i<n;++i){
scanf("%d",&pos[i]);
}
for(i=;i<n;++i){
scanf("%d",&in[i]);
}
root=buildTree(,n-,,n-);
printLevelOrder(root);
return ;
}
the main function is rebuild the tree,which is a recursive function;and it is important to find
the left side and the right side from both postOrder and InOrder!Therefore ,we can use these argument to build the tree recursively.
1020. Tree Traversals (25)的更多相关文章
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020. Tree Traversals (25) -BFS
题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
- 1020 Tree Traversals (25)(25 point(s))
problem Suppose that all the keys in a binary tree are distinct positive integers. Given the postord ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
随机推荐
- [Web API] 如何让 Web API 统一回传格式以及例外处理[转]
[Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ...
- CSS基础知识—【结构、层叠、视觉格式化】
结构和层叠 选择器的优先级顺序: style[内联元素]选择器>Id选择器>类选择器 属性选择器>元素选择器>通配器选择器 重要性:@important 有这个标记的属性值,优 ...
- 在虚拟机中安装windows
前言: 本来在windows当中安装windows是一件很简单的事,但是在使用光盘进行安装的时候,发现无法进行安装. 思路: 将光盘进行提取成iso文件,一个光盘提取一个iso文件,从而存在两个iso ...
- Android多媒体--MediaCodec的实例化方法
*由于作者水平限制,文中难免有错误和不恰当之处,望批评指正. *转载请注明出处:http://www.cnblogs.com/roger-yu/ MediaCodec的实例化方法主要有两种: 1.使用 ...
- SSH与SSL
1. SSL SSH 即Secure Shell,它主要由三部分组成: 第一部分:连接协议 [SSH-CONNECT] 将多个加密隧道分成逻辑通道.它运行在用户认证协议上.它提供了交互式登录话路.远程 ...
- UVALive 7457 Discrete Logarithm Problem (暴力枚举)
Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...
- OLEVARIANT的替代——FIREDAC的TFDJSONDataSets和TFDJSONDeltas
OLEVARIANT——这个COM的序列格式,也是DATASNAP已使用了20年的序列格式,在20年以后的今天,终于有了它的替代者:FIREDAC的TFDJSONDataSets和TFDJSONDel ...
- Quality Center配置邮箱服务
Quality Center上要配置二个地方 mail direct pro配置 DNS地址是本机的地址就好了,不需要真实的DNS地址 SMTP端口使用普通的25就好了,不需要使用SSL的·465端口 ...
- python中列表,元组,字符串如何互相转换
python中有三个内建函数:列表,元组和字符串,他们之间的互相转换使用三个函数,str(),tuple()和list(),具体示例如下所示: >>> s = "xxxxx ...
- hdu 4763 Theme Section(KMP水题)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...