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. 第06篇 MEF部件的生命周期(PartCreationPolicy)

    一.演示概述 本演示介绍了MEF的生命周期管理,重点介绍了导出部件的三种创建策略,分别是:CreationPolicy.Any.CreationPolicy.Shared.CreationPolicy ...

  2. 异常:java.lang.IllegalArgumentException: Control character in cookie value or attribute.

    后台提示: 严重: Error processing requestjava.lang.IllegalArgumentException: Control character in cookie va ...

  3. (转)[Android实例] 关于使用ContentObserver监听不到删除短信会话的解决方案

    最近做通讯录的项目,需要实时监听短信的删除,就用到了观察者ContentObserver,怪异的事情就此发生,当我删除一条短信的时候,可以监听到,但是,当我删除整条短信的时候,就无法监听到,查了很多资 ...

  4. ThinkPHP 分页功能梳理

    最近在开发一个项目,使用了国内流行的ThinkPHP框架,我之前没怎么用过这个框架,也是临时抱佛脚,用的不怎么样?可能理解不是很深刻,如果有说的不对或不正确的地方,请大家多包涵,多指教. ThinkP ...

  5. 给scrapy添加代理IP

    request.meta['proxy'] = 'http://'+'175.42.123.111:33995'

  6. Nginx限制服务地址

    今天要在Nginx上设置禁止通过IP访问服务器,只能通过域名访问,这样做是为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,从网络上搜到以下解决方案. 我们在使用的时候会遇到很多的恶 ...

  7. 小程序scroll-view组件使用时,子元素虽设置样式display:inline-flex;whit-space:nowrap

    小程序scroll-view组件使用时,子元素虽设置样式display:inline-flex;whit-space:nowrap

  8. Centos如何设置IP地址,LINUX怎么修改IP地址

    对于很多刚刚接触linux的朋友来说,如何设置linux系统的IP地址,作为第一步,下面小编以centos系统为例,给大家演示如何给centos设置IP地址,如何修改linux 系统IP地址? 步骤阅 ...

  9. 仅用CSS3创建h5预加载雷达圈

    <head> <meta charset="UTF-8"> <title></title> <style type=" ...

  10. 转 :关于springmvc使用拦截器

    原博客: http://elim.iteye.com/blog/1750680 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的 ...