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): 4210    Accepted Submission(s): 1908

Problem Description
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed 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.

 
Input
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
 
Output
For each test case print a single line specifying the corresponding postorder sequence.
 
Sample Input
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
 
Sample Output
7 4 2 8 9 5 6 3 1
 

题解:意思很直白。给你二叉树的先序序列和中序序列,让你求后序序列。先利用先序序列和中序序列的特征递归建树,再后序遍历。

代码:

 #include <fstream>
#include <iostream>
#include <cstdio> using namespace std; const int N=;
struct node{
int c;
struct node *lch,*rch;
};
int n,tn;
int pre[N],in[N]; void createTree(int* l,int* r,int i,int j,int e,int f,node** tree);
void postOrder(node *p); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
node *tree;
while(~scanf("%d",&n)){
for(int i=;i<n;i++)
scanf("%d",&pre[i]);
for(int i=;i<n;i++)
scanf("%d",&in[i]);
createTree(pre,in,,n-,,n-,&tree);
tn=;
postOrder(tree);
}
return ;
}
void createTree(int* l,int* r,int i,int j,int e,int f,node** tree){//可以思考下这里为什么是**,可不可以用*?
int m;
(*tree)=new node;
(*tree)->c=l[i];
m=e;
while(r[m]!=l[i]) m++;
if(m==e) (*tree)->lch=NULL;
else createTree(l,r,i+,i+m-e,e,m-,&(*tree)->lch);
if(m==f) (*tree)->rch=NULL;
else createTree(l,r,i+m-e+,j,m+,f,&(*tree)->rch);
}
void postOrder(node *p){
if(p!=NULL){
postOrder(p->lch);
postOrder(p->rch);
printf("%d",p->c);
if(++tn<n) printf(" ");
else puts("");
delete p;
}
}

hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)的更多相关文章

  1. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)

                                                                                Binary Tree Traversals T ...

  4. HDU 1710 Binary Tree Traversals(二叉树遍历)

    传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...

  5. hdu1710 Binary Tree Traversals(二叉树的遍历)

    A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjo ...

  6. hdu1710 Binary Tree Traversals

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1710 题意:给前序.中序求后序,多组 前序:根左右 中序:左右根 分析:因为前序(根左右)最先出现的总 ...

  7. Binary Tree Traversals(HDU1710)二叉树的简单应用

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  8. HDU 1710 二叉树的遍历 Binary Tree Traversals

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. HDU 1710 Binary Tree Traversals(二叉树)

    题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...

  10. HDU-1701 Binary Tree Traversals

    http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...

随机推荐

  1. Serf 了解

    Introduction to Serf Welcome to the intro guide to Serf! This guide will show you what Serf is, expl ...

  2. iOS7 自己定义动画跳转

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/Liar0606/article/details/26399125 简单介绍 在iOS7系统中,假设你 ...

  3. 使用ILMerge将源DLL合并到目标EXE(.NET4.6.2)

    本文为原创文章,如转载,请在网页明显位置标明原文名称.作者及网址,谢谢! 本文主要是使用微软的ILMerge工具将源DLL合并到目标EXE,因此,需要下载以下工具: https://www.micro ...

  4. 抽象工厂模式(abstract)创建型模式

    (一)简单工厂模式? 现在的学习是面向对象面向接口的,但是执行时的操作需要实例化后的对象.随着我们需要的类的增加,我们就需要把这些共同的东西提取出来,放在一个抽象类中,让这些子类来继承抽象类.当我们调 ...

  5. OkHttp使用方法

    1.在app/build.gradle中添加依赖 compile 'com.squareup.okhttp3:okhttp:4.0.1' 2.创建OkHttpClient实例 OkHttpClient ...

  6. 慢日志之二:ERROR 1146 (42S02): Table 'mysql.slow_log' doesn't exist,分析诊断工具之四

    去查看最新的slow log,发现没有最新的记录,上去检查slow log是否开启了. MySQL> show variables like '%slow%'; +--------------- ...

  7. Java-Runoob:Java Stream、File、IO

    ylbtech-Java-Runoob:Java Stream.File.IO 1.返回顶部 1. Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出 ...

  8. scrapy_redis 实现多进程配置部分代码

    # 启用Redis调度存储请求队列SCHEDULER = "scrapy_redis.scheduler.Scheduler"# 确保所有的爬虫通过Redis去重DUPEFILTE ...

  9. Mysql无法创建外键的原因 !!!

    在MySQL中创建外键时,经常会遇到问题而失败,这是因为Mysql中还有很多细节需要我们去留意,我自己总结并查阅资料后列出了以下几种常见原因. 1.  两个字段的类型或者大小不严格匹配.例如,如果一个 ...

  10. 27 mysql主从出现错误

    大多数的互联网应用场景都是读多写少,在发展过程中可能会出现读性能问题,在数据库层解决读性能问题:一主多从 下面是多主从结构 虚线箭头表示主备关系,A与A’互为主备,从库B,C,D指向主库A,一主多从的 ...