Binary Tree Traversals(HDU1710)二叉树的简单应用
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2681 Accepted Submission(s): 1178
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.

#include<iostream>
using namespace std; typedef struct tree// typedef定义类型,这里为结构体型;
{
int num;
struct tree *left;
struct tree *right;
}; tree *root; tree *creat(int *a,int *b,int n)//用前序历遍和中序历遍得到的数据,确定唯一树 {
tree *k;
int i;
for(i=;i<n;i++)
{
if(*a==b[i])//当在中序历遍中找到了根节点后
{
k=(tree *)malloc(sizeof(tree));
k->num=b[i];
k->left=creat(a+,b,i);//中序历遍中在根节点左边的都是左子树上的
k->right=creat(a+i+,b+i+,n-i-);//在根节点右边的,都是右子树上的,右子树需要从i+1开始;
//因为他的根的左半只有i个数,加上自己所有就要把指针指向a+i+1的地方了, // printf("%d\n",h->data );直接输出于是后续,不过要判断不成立的情况,所以不行不过我想可以用数组装起来;
return k;
}
}
return NULL;//没有对应找到的话,就返回NULL
} void houxu(tree *h)
{
if(h!=NULL)
{
houxu(h->left);
houxu(h->right);
if(root==h)//后序历遍最后历遍根节点
{
printf("%d\n",h->num);
}
else
{
printf("%d ",h->num);
}
}
}
int main()
{
tree *h;
int a[],b[],n,i;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=;i<n;i++)
scanf("%d",&a[i]);
for(i=;i<n;i++)
scanf("%d",&b[i]);
h=creat(a,b,n);
root=h;
houxu(h);
}
}
Binary Tree Traversals(HDU1710)二叉树的简单应用的更多相关文章
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- 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 ...
- HDU-1701 Binary Tree Traversals
http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 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 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...
随机推荐
- eclipse代码提示javadoc背景为黑色框的解决办法
我的eclipse是近期下载的oxygen版本.不知道怎么出现了一个这个问题,鼠标悬停指向代码时应该出现的代码提示解释框,全为黑色,看不到文字.如下图 经过验证,最终解决方法为window->G ...
- CentOS7安装MYSQL。
参考这个文章(网页已存到本地):http://www.cnblogs.com/starof/p/4680083.html 安装完成后,本地登录MYSQL没有问题. 现在主要是在windows下用ora ...
- vue-cli项目配置文件分析
最近在vue-cli生成的webpack模板项目的基础上开发了一些项目,开发过程中遇到很多坑,并且需要改动build和config里面一些相关的配置,查阅,学习,总结,分享. 一.配置文件结构 本文主 ...
- Spark中的常用算子
更多有用的例子和算子讲解参见: http://homepage.cs.latrobe.edu.au/zhe/ZhenHeSparkRDDAPIExamples.html map是对每个元素操作, ma ...
- android开发学习——day1
了解安卓系统架构:Linux内核层,系统运行层库,应用框架层,应用层 版本信息 android开发的特色之处就在于强大的组件功能 开发环境android stdio 2.0安装:把安装的组件都勾选上, ...
- C#:struct的陷阱:无法修改“xxx”的返回值,因为它不是变量
示例代码如下: public struct SpiderResult { public string robotName; public string RobotName { get { return ...
- Java抽象类应用—模板方法模式
模板方法模式(Templete method) 定义一个操作中的算法的骨架,而将一些可变部分的实现延迟到子类中,模板方法模式使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定的步骤. 例: ...
- Unity学习系列一简介
一.简介 Unity的目标是为了提升"依赖注入"的思想,去建立更加松耦合的系统.patterns & practices 小组在那个时候实现DI的方式和我们现在认为的DI有 ...
- JVM 监控工具 jstack 和 jvisualvm 的使用
Java线程状态 线程的五种状态 * 新建:new(时间很短) * 运行:runnable * 等待:waitting(无限期等待),timed waitting(限期等待) * 阻塞:blocked ...
- RESTful SOA与DDD(领域驱动设计)
视频地址:http://www.infoq.com/presentations/RESTful-SOA-DDD 作者的一个DDD采访:http://www.informit.com/articles/ ...