HDU_1710_二叉树前序中序确定后序
2018-3-6
按照王道机试书上的思路再做了一遍,先根据先序和中序建树,然后后序遍历。
静态分配数组用于建树,可以返回数组地址当作结点指针。
#include<iostream>
#include<cstdio>
using namespace std; int pre[],in[],n; struct Node
{
Node *lson,*rson;
int num;
}node[]; //静态分配一个数组用于建树 int cntn; Node *create(int num)
{
node[cntn].lson=node[cntn].rson=NULL;
node[cntn].num=num;
return &node[cntn++]; //返回数组元素的地址
} Node *build(int ph,int ih,int len)
{
Node *now=create(pre[ph]);
int i;
for(i=;pre[ph]!=in[i];i++);
if(i!=ih)
now->lson=build(ph+,ih,i-ih);
if(i!=ih+len-)
now->rson=build(ph+i-ih+,i+,len-(i-ih)-);
return now;
} int cnt;
void postOrder(Node *rt)
{
if(rt==NULL)
return;
postOrder(rt->lson);
postOrder(rt->rson);
printf("%d",rt->num);
if(++cnt==n)
printf("\n");
else
printf(" ");
} int main()
{
while(scanf("%d",&n)!=EOF)
{
cntn=cnt=;
for(int i=; i<=n; i++)
scanf("%d",&pre[i]);
for(int i=; i<=n; i++)
scanf("%d",&in[i]);
Node *rt=build(,,n);
postOrder(rt);
}
return ;
}
2018-3-3
又做了一遍,这次是自己根据思想自己码了一遍
看了上次的代码,感觉还是上次的好一点
#include<iostream>
#include<cstdio>
using namespace std; int pre[],in[],cnt,n; void getPost(int pl,int pr,int il,int ir)
{
if(pr<pl||ir<il)
return;
int loc;
for(int i=il; i<=ir; i++)
if(in[i]==pre[pl])
{
loc=i;
break;
}
if(<loc-il)
getPost(pl+,pl+loc-il,il,loc-);
if(<ir-loc)
getPost(pr-(ir-loc)+,pr,loc+,ir);
printf("%d",pre[pl]);
cnt++;
if(cnt==n)
printf("\n");
else
printf(" "); }
int main()
{
while(scanf("%d",&n)!=EOF)
{
cnt=;
for(int i=; i<=n; i++)
scanf("%d",&pre[i]);
for(int i=; i<=n; i++)
scanf("%d",&in[i]);
getPost(,n,,n);
}
return ;
}
2018-1-3
研究生考试初试结束一个周了,开始准备复试了,又要开始刷题了。
给定一个二叉树前序和中序,确定后序。
#include<iostream>
#include<cstdio>
using namespace std; int pre[],in[]; void getPostOrder(int ph,int ih,int len,int flag)
{
if(len<)
return;
int i;
for(i=; pre[ph]!=in[ih+i]; i++); //在中序中找到根节点
getPostOrder(ph+,ih,i,); //处理左子树
getPostOrder(ph+i+,ih+i+,len-i-,); //处理右子树
if(flag)
printf("%d\n",pre[ph]);
else
printf("%d ",pre[ph]);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<n; i++)
scanf("%d",&pre[i]);
for(int i=; i<n; i++)
scanf("%d",&in[i]);
//cout<<"*"<<endl;
getPostOrder(,,n,);
}
return ;
}
HDU_1710_二叉树前序中序确定后序的更多相关文章
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- 二叉树的前序和中序得到后序 hdu1710
今天看学长发过来的资料上面提到了中科院机试会有一个二叉树的前序中序得到后序的题目.中科院的代码编写时间为一个小时,于是在七点整的时候我开始拍这个题目.这种类型完全没做过,只有纸质实现过,主体代码半个小 ...
- Java实现二叉树的前序、中序、后序遍历(非递归方法)
在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...
- LeetCode二叉树的前序、中序、后序遍历(递归实现)
本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...
- c/c++ 用前序和中序,或者中序和后序,创建二叉树
c/c++ 用前序和中序,或者中序和后序,创建二叉树 用前序和中序创建二叉树 //用没有结束标记的char*, clr为前序,lcr为中序来创建树 //前序的第一个字符一定是root节点,然后去中序字 ...
- Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)
在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...
- 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...
- PHP递归方法实现前序、中序、后序遍历二叉树
二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). class Node { public $value; pub ...
- 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)
将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...
随机推荐
- bzoj1486【HNOI2009】最小圈
1486: [HNOI2009]最小圈 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1778 Solved: 827 [Submit][Statu ...
- YTU 1098: The 3n + 1 problem
1098: The 3n + 1 problem 时间限制: 1 Sec 内存限制: 64 MB 提交: 368 解决: 148 题目描述 Consider the following algor ...
- lmbench andlmbench 移植测试
/*********************************************************************** * lmbench andlmbench 移植测试 * ...
- 【SCOI 2005】 互不侵犯
[题目链接] 点击打开链接 [算法] 和HDU2167类似 先搜出一行内符合的状态,然后,f[i][j][k]表示第i行,第j种状态,放了k个,合法的方案,DP即可 [代码] #include< ...
- bzoj 1731 Layout 排队布局 —— 差分约束
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 差分约束: ML: dis[y] - dis[x] <= k,即 x 向 y 连 ...
- Python基础第八天
一.内容
- Extjs 3 TreePanel相关操作
这里说的选中是指鼠标点击一个节点后,节点那一行出现浅蓝色背景的选中,而非checkbox的勾选 方法 Ext.tree.TreePanel.getSelectionModel().getSelecte ...
- tar.xz格式文件的解压方法(转载)
转自:http://bbs.chinaunix.net/thread-3610738-1-1.html 现在很多找到的软件都是tar.xz的格式的,xz 是一个使用 LZMA压缩算法的无损数据压缩文件 ...
- 【原创】Elasticsearch无宕机迁移节点
官方API文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/allocation-filtering.html 参考 ...
- E20170524-hm
logging n. <美>伐木搬运业; 记录,存入; 航行日志; inversion n. 倒置; 转化; (尤指词序) 倒装; (染色体的) 倒位; reversion n. ...