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<iostream>
using namespace std;
int t1[],t2[];
void sousuo(int a,int b,int n,int flag)
{ if(n==)//如果存在左子树或右子树就直接输出
{
printf("%d ",t1[a]);
return ;
}
else if(n<=)//如果不存在左子树或右子树就返回上一层
return ;
int i;//继续罚分为左子树和右子树
for(i=;t1[a]!=t2[b+i];i++) ;//找到罚分点也就是根节点
sousuo(a+,b,i,);//左子树的遍历
sousuo(a+i+,b+i+,n-i-,);//右子树的遍历
if(flag==)//最原始的跟节点
printf("%d",t1[a]);
else//一般的根节点
printf("%d ",t1[a]);
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
scanf("%d",&t1[i]);//t1中存的是前序
for(i=;i<=n;i++)//t2中存的中序
scanf("%d",&t2[i]);
sousuo(,,n,);
printf("\n");
}
return ;
}

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(二叉树)

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

  3. hdu1710 Binary Tree Traversals

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

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

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

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

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

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

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

  7. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  8. hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

    题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...

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

                                                                                Binary Tree Traversals T ...

随机推荐

  1. jq 全选和反选以及判断那条被选中

    <body><div><input type="checkbox" id="a" />全选</div><d ...

  2. mysql可以用这种方式<<! 输入内容 ! 做成脚本

    以这种文件式做交接NB!!!!! [root@NB test]# mysql -uroot -p$passwd <<! > use mysql > select user,ho ...

  3. CI重定向:php(codeigniter)中如何重定向

    Q: 在保存完数据之后需要重定向,防止数据重复提交. 我使用$this->方法名();跳转,发现不能达到重定向的效果(地址栏没变) 请教高手重定向怎么用 A: $this->load-&g ...

  4. HTML+CSS页面滚动效果处理

    HTML+CSS代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> ...

  5. 企业QQ 增加在线交谈链接

    企业QQ的在线交流链接跟普通QQ的在线交流不一样,普通QQ的在线交流,可以在http://shang.qq.com/v3/widget.html生成:企业qq的链接可以按以下步骤添加: 第一步:引入企 ...

  6. 向Word模板中填充数据

    现在有这样的需求,给Word文档的指定位置填充上特定数据,例如我们有一个终端,用来打印员工的薪资证明,对于一个公司来说,他的薪资证明模板是固定的,变化的地方是员工姓名,部门,职位等.我们只需要将这些指 ...

  7. [JavaCore] 不错的Java基础学习资料-持续更新

    容易弄混的JAVA基础知识: http://www.iteye.com/topic/943647 [总结]String in Java: http://www.iteye.com/topic/5221 ...

  8. 报错:1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost

    执行如下命令报错 mysql -uroot -h${hostIp} -p Enter password:********* ERROR (HY000): Host '$hostIp' is not a ...

  9. C语言文件操作fclose在NDK引起的BUG

    今天在NDK中写了一个简单的写入文件操作: FILE *fp = fopen("/sdcard/test.txt","w"); if(fp == NULL) { ...

  10. CentOS配置本地yum源(使用镜像iso文件)

    本人在使用yum安装软件的时候,感觉最不爽的是网络不佳时,安装的速度特别慢.所以,个人就上网search了一下如何使用Linux的安装文件作为其yum源.经过几次尝试,已经可以成功的配置了.下面是详细 ...