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 ...
随机推荐
- HTML 5:绘制旋转的太极图
HTML: <!DOCTYPE> <html> <head> <meta charset="utf-8" /> <title& ...
- 关于FireFox下 CSS3 transition 与其他浏览器的差异
最近一个项目,动画效果全靠CSS3来做,用得比较多的transition,发现了一点火狐与其他浏览器的小差异. 首先我们写CSS的时候,一般为属性值为0的属性,我们一般会这样写 #id{ posito ...
- 如何在 Windows Azure 的虚拟机 ubuntu 上面安装和配置 openVPN(三)
第三步:安装openVPN 请打开 ubuntu 官方文档.然后,从上到下,按照步骤,输入执行命令.大家只需要执行到 First trouble shooting 之前即可. 不要怕麻烦,仔细读,一步 ...
- Unbutu网卡驱动安装(Intel内置网卡8086:15b8)
工作中需要在新的实验平台上安装unbuntu14.04操作系统,系统安装好之后发现无法连接网络,分析后是由于缺少网卡驱动的原因. 下面把分析问题的过程及安装网卡驱动步骤介绍如下: 查看PCI信息 su ...
- Apache Rewrite常用设置说明
例子: RewriteEngine on 打开引擎 RewriteRule test.html /test.php [L] RewriteRule test.html?$ /tianqi.php?s1 ...
- <Araxis Merge>快速一览文件的比较与合并
重要的文件比较与合并特性在下面都指出了.对每个特性的说明性内容在下面可以找到. 注意:只有双向的比较/合并被展示了,专业版的Merge还支持三向的比较/合并. 1.文件夹比较按钮 单击这个工具栏按钮会 ...
- 软件开发杂谈之从需求到上线---valen
背景 IT已经成为当代企业必不可少的竞争手段,从无到有到标配,可以说以后不懂IT的就是文盲这句一点也不过,而软件开发是个复杂工程,零零碎碎各种理论工具和技巧,一言难尽. 本文意在言简意赅,简述软件开发 ...
- delphi回调函数
文章来源: http://anony3721.blog.163.com/blog/static/5119742010866050589/ 一.主单元 unit UnMain; interface us ...
- JSF 2 password example
In JSF, you can use the <h:inputSecret /> tag to render a HTML input of type="password&qu ...
- jquery easyui的异步tree
1.创建一个简单的tree 结果如图: <script> $(function(){ $('#tt').tree(){ url:'要提交的url地址', checkbox:true, li ...