hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4205 Accepted Submission(s): 1904
or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.
In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.
In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.
In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.
Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
they are always correspond to a exclusive binary tree.
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
7 4 2 8 9 5 6 3 1
然后这个根把中序分为两半,左边是左子树,右边是右子树。
然后递归下就ok了。
#include <cstdio>
#include <algorithm>
using namespace std; struct tree
{
struct tree* l;
struct tree* r;
int val;
tree()
{
l=NULL;
r=NULL;
}
}; int pre[1010],in[1010];
int n;
tree* root;
void build(int num,int l,int r,tree *rt)//pre num in_l in_r
{
int flag=-1;
while(flag==-1)
{
for(int i=l;i<=r;i++)
{
if(pre[num]==in[i])
flag=i;
}
if(flag==-1)
num++;
}
if(flag-l>0)
{
rt->l=(tree*)malloc(sizeof(tree));
*(rt->l)=tree();
build(num+1,l,flag-1,rt->l);
}
if(r-flag>0)
{
rt->r=(tree*)malloc(sizeof(tree));
*(rt->r)=tree();
build(num+1,flag+1,r,rt->r);
}
rt->val=pre[num];
} void post(tree* nw)
{
if(nw->l!=NULL)
post(nw->l);
if(nw->r!=NULL)
post(nw->r); if(nw==root)
printf("%d",nw->val);
else
printf("%d ",nw->val); } void del(tree* nw)
{
if(nw->l!=NULL)
del(nw->l);
if(nw->r!=NULL)
del(nw->r);
free(nw);
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&pre[i]);
for(int i=0;i<n;i++)
scanf("%d",&in[i]);
root=(tree*)malloc(sizeof(tree));
*root=tree();
build(0,0,n-1,root);
post(root);
puts("");
del(root);
}
return 0;
}
hdu 1710 Binary Tree Traversals 前序遍历和中序推后序的更多相关文章
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- 【二叉树】hdu 1710 Binary Tree Traversals
acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...
- HDU 1710 Binary Tree Traversals
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...
- HDU 1710 Binary Tree Traversals(二叉树)
题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...
- hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- 《零基础入门学习Python》【第一版】视频课后答案第005讲
测试题答案: 0.Python中: int:整型 bool:布尔类型 float:浮点型 str:字符串类型 1.为什么布尔类型(bool)的TRUE和FALSE分别用0和1表示? 计算机只认识二进制 ...
- 前端Web框架的实现过程
一.Web框架的本质: 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 i ...
- UI测试点
UI测试点 1.界面是否美观 2.元素大小 3.界面元素是否对齐方式统一 4.界面字体属性是否正确 5.界面链接及触发动作 6.元素内容是否显示正确.易懂.友好 7.所有输入框进行输入判断测试 8.所 ...
- PHP 页面跳转的三种方式
第一种方式:header() header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. 语法: void header ( string $string [, bool $re ...
- ImportError: No module named ‘MySQLdb'
1.APP未注册 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contentty ...
- hdu2051
二进制转换 #include <stdio.h> void change(int n){ ]; ; while(n){ num[cnt]=n%; n/=; cnt++; } cnt--; ...
- ajax 下拉加载更多效果
1.生成HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- [BZOJ2342] [Shoi2011]双倍回文(manacher)
传送门 manacher...... 先跑一边manacher是必须的 然后枚举双倍回文串的对称轴x 把这个双倍回文串分成4段,w wR w wR 发现,只有当 y <= x + p[x] / ...
- hibernate的cascade问题
cascade属性的可能值有 all: 所有情况下均进行关联操作,即save-update和delete. none: 所有情况下均不进行关联操作.这是默认值. save-update: 在执行sav ...
- 转:WOM 编码与一次写入型存储器的重复使用
转自:WOM 编码与一次写入型存储器的重复使用 (很有趣的算法设计)——来自 Matrix67: The Aha Moments 大神 计算机历史上,很多存储器的写入操作都是一次性的. Wikiped ...