题链;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): 4205    Accepted Submission(s): 1904

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
 

题意:给出前序历遍和中序遍历的顺序,来推导后序遍历的顺序。
做法:把握两个要点。前序是 先根历遍的。所曾经序的第一个是根。

然后这个根把中序分为两半,左边是左子树,右边是右子树。

然后递归下就ok了。


#include <cstdio>
#include <algorithm>
using namespace std; struct tree
{
struct tree* l;
struct tree* r;
int val;
tree()
{
l=NULL;
r=NULL;
}
}; int pre[1010],in[1010];
int n;
tree* root;
void build(int num,int l,int r,tree *rt)//pre num in_l in_r
{
int flag=-1;
while(flag==-1)
{
for(int i=l;i<=r;i++)
{
if(pre[num]==in[i])
flag=i;
}
if(flag==-1)
num++;
}
if(flag-l>0)
{
rt->l=(tree*)malloc(sizeof(tree));
*(rt->l)=tree();
build(num+1,l,flag-1,rt->l);
}
if(r-flag>0)
{
rt->r=(tree*)malloc(sizeof(tree));
*(rt->r)=tree();
build(num+1,flag+1,r,rt->r);
}
rt->val=pre[num];
} void post(tree* nw)
{
if(nw->l!=NULL)
post(nw->l);
if(nw->r!=NULL)
post(nw->r); if(nw==root)
printf("%d",nw->val);
else
printf("%d ",nw->val); } void del(tree* nw)
{
if(nw->l!=NULL)
del(nw->l);
if(nw->r!=NULL)
del(nw->r);
free(nw);
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&pre[i]);
for(int i=0;i<n;i++)
scanf("%d",&in[i]);
root=(tree*)malloc(sizeof(tree));
*root=tree();
build(0,0,n-1,root);
post(root);
puts("");
del(root);
}
return 0;
}

hdu 1710 Binary Tree Traversals 前序遍历和中序推后序的更多相关文章

  1. HDU 1710 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 1710 Binary Tree Traversals(二叉树遍历)

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

  4. 【二叉树】hdu 1710 Binary Tree Traversals

    acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...

  5. HDU 1710 Binary Tree Traversals

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...

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

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

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

                                                                                Binary Tree Traversals T ...

  8. hdu1710(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

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

随机推荐

  1. 关于django form验证是否用户名已存在

    想通过django的Form模块进行数据库中是否已存在用户名的验证,首先我先调用了数据库用户名字段所有的值,发现是个queryset对象. 随后经过查询后发现queryset查询集对象可以调用list ...

  2. luogu3809 后缀排序 后缀数组

    ref and 挑战程序设计竞赛. 主要是发现自己以前写得代码太难看而且忘光了,而且我字符串死活学不会啊,kmp这种东西我都觉得是省选+难度啊QAQ #include <iostream> ...

  3. IOS 自动布局-UIStackPanel和UIGridPanel(四)

    为什么说scrollview的自动化布局是难点? 对scrollview做自动化布局,无非就是想对scrollview里面的subviews来做自动化布局.但是scrollview里面的subview ...

  4. 大数据学习——sqoop导出数据

    把数据从hadoop导出到关系型数据库 将数据从HDFS导出到RDBMS数据库 导出前,目标表必须存在于目标数据库中. u  默认操作是从将文件中的数据使用INSERT语句插入到表中 u  更新模式下 ...

  5. ACdream 1135 MST

    MST Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Problem Descrip ...

  6. linux下连接到远程主机,用图像界面(想在远程服务器上用cmake)

    1. 需要通过SSH -X username@ip登陆服务器后,再用图形界面,比如用cmake 2.直接用 SSH username@ip命令登陆服务器后,不能用cmake

  7. z作业二总结

    这是我的第二次作业,之前在课上所学的我发现已经忘得差不多了,这次的作业让我做的非常累,感觉整个人生都不太好了. 作业中的知识点:int(整型) float(单精度) double(双精度) char( ...

  8. 只操作git(cmd)就可以使用git将项目上传到github

    代码改变世界 使用git将项目上传到github(最简单方法) 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具, ...

  9. 转载 cc、gcc、g++、CC的区别概括

    gcc是C编译器:g++是C++编译器:linux下cc一般是一个符号连接,指向gcc:gcc和g++都是GUN(组织)的编译器.而CC则一般是makefile里面的一个名字,即宏定义,嘿,因为Lin ...

  10. PHPstorm注册码(7.1.3)

    UserName EMBRACE ===== LICENSE BEGIN ===== 18710-12042010 00000EsehCiFamTQe"7jHcPB16QOyk S" ...