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 ...
随机推荐
- 过滤器中获取form表单或url请求数据
var httpFormData = filterContext.HttpContext.Request.Form; var logContent = string.Empty; //获取url的 l ...
- vue.js - 2
最近开发公司vue前端项目,做一下笔记,偶尔上来查漏补缺 组件操作: 使用flag标识符结合v-if和v-else切换组件 页面结构: <div id="app"> & ...
- 友链&&日记
上面友链,下面日记 友人链 最喜欢galgameの加藤聚聚 初三一本&&\(ACG\)姿势比我还丰厚的yx巨巨 更喜欢galgame的shadowice czx ZigZag胖胖 文文 ...
- Python 生成器的使用(yield)
一. 生成器就是一个特殊的迭代器, 使用关键字yield就可以生成一个生成器 def func(): for i in range(10): yield i item = func() yield i ...
- webstorm “Unterminated statement”
使用webstorm的时候,写console.log,或者一些其他语句的时候 偶尔会出现这种提示,不是报错,就是看着别扭,应该是写法规范问题. 解决办法: 在下面空一行就行了
- vue-cli初始化一个项目
1,换成淘宝源: npm config set registry https://registry.npm.taobao.org/ 检查是否修改成功 npm config get registry 2 ...
- lucene使用与优化
lucene使用与优化 1 lucene简介 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿 ...
- 个人网站搭建时linux中的相关配置记录(mysql,jdk,nginx,redis)
一.开发计划(包括准备工作,网站大致需求等) 二.服务器(linux/centos)购买.相应环境配置(jdk),软件安装(mysql, nginx, redis).域名解析 三.原型图.代码开发(v ...
- Hbuilder用ajax连接阿里服务器上的servlet以及注意事项
Hbuiler连接服务器上的servlet的步骤与连接本地项目中的servlet基本一致,详细内容参考上一片博客:https://www.cnblogs.com/ljysy/p/10294640.ht ...
- asp.net core 系列之用户认证(1)-给项目添加 Identity
对于没有包含认证(authentication),的项目,你可以使用基架(scaffolder)把 Identity的程序集包加入到项目中,并且选择性的添加Identity的代码进行生成. 虽然基架已 ...