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 ...
随机推荐
- solr 5.5使用 和pyg里 的4.10.3版 部署到tomcat中不一样(不使用内置jetty)
http://www.cnblogs.com/zhuxiaojie/p/5764680.html
- java 路径的问题
在项目开发中会碰到各种各样的获取项目路径的一些问题: 1:java项目: 以获取 类路径下的mess.properties 为例来说明: 文件在项目中的位置: src/bz/beppe/demo/r ...
- Mac OSX配置XAMP虚拟主机
在前端工作中,有时候需要配置下环境,这篇文章主要是记录了在wamp中配置虚拟主机 首先需要下载wamp软件和navicat数据库管理软件进行管理,下面默认已经下载好所需软件.并且打开服务. 第一步:把 ...
- Linux - route & traceroute & ip
route route - show / manipulate the IP routing table route 命令常用命令示例 #显示路由 route route -n # 不解析名字,快速显 ...
- RegexHelper.js
var Validator = { VerityLib: { //验证字符串非空 IsNotEmpty: function (input) { if (input != '') { return tr ...
- Flask 微博三方登录
1.进入微博开发平台(https://open.weibo.com/),注册账号,填写开发者信息 2.创建应用.创建应用完成后,可以在"我的应用 - 应用信息"中查看您所创建应用的 ...
- 56.storm 之 hello world (集群模式)
回顾 在上一小节,我们在PWTopology1 这一个java类中注解掉了集群模式,使用本地模式大概了解一下storm的工作流程.这一节我们注解掉本地模式相关的代码,放开集群模式相关代码,并且将项目打 ...
- odoo开发笔记 -- 用户配置界面如何增加模块访问权限
在odoo设置界面,点击用户,进入用户配置界面,会看到: 访问权 | 个人资料菜单 在访问权 page菜单界面,可以看到系统预制的一些模块都会显示在这里, 那么,我们自己开发的模块如何显示在这块呢,从 ...
- Oracle RAC 环境下的连接管理(转) --- 防止原文连接失效
崔华老师的文章!!! 这篇文章详细介绍了Oracle RAC环境下的连接管理,分别介绍了什么是 Connect Time Load Balancing.Runtime Connection Load ...
- Oracle修改指定表空间为自动扩展
1.数据文件自动扩展的好处 1)不会出现因为没有剩余空间可以利用到数据无法写入 2)尽量减少人为的维护 3)可以用于重要级别不是很大的数据库中,如测试数据库等 2.数据文件自动扩展的弊端 1)如果任其 ...