hdu1710

题目地址:https://acm.dingbacode.com/showproblem.php?pid=1710

(最近几天杭电原网址开不进去了,之后应该可以通。。吧)

Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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
题目大意
 输入二叉树的先序和中序遍历序列,求后序遍历
 输入样例
  先序:1 2 4 7 3 5 8 9 6
  中序:4 7 2 1 8 5 9 3 6
 输出样例
  后序:7 4 2 8 9 5 6 3 1
 
AC代码

 1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=1010;
4 int pre[N],in[N],post[N];
5 int k;
6 struct node{
7 int value;
8 node *l,*r;
9 node(int value = 0, node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
10 };
11
12 void buildtree(int l,int r,int &t,node *&root){ //建树,一定是*&root!!!!!!!!!!!!
13 int flag=-1;
14 for(int i=l;i<=r;i++){ //字母l-r,误敲成数字1也A了。。
15 if(in[i]==pre[t]){ //找到先序中的根在中序里的位置
16 flag=i; //存在flag中
17 break;
18 }
19 }
20 if(flag==-1) return; //结束
21 root=new node(in[flag]); //新建结点
22 t++;
23 if(flag>l) buildtree(l,flag-1,t,root->l);
24 if(flag<r) buildtree(flag+1,r,t,root->r);
25 }
26
27 void preorder(node *root){ //先序遍历
28 if(root!=NULL){
29 post[k++]=root->value; //输出
30 preorder(root->l);
31 preorder(root->r);
32 }
33 }
34
35 void inorder(node *root){ //中序遍历
36 if(root!=NULL){
37 inorder(root->l);
38 post[k++]=root->value;
39 inorder(root->r);
40 }
41 }
42
43 void postorder(node *root){ //后序遍历
44 if(root!=NULL){
45 postorder(root->l);
46 postorder(root->r);
47 post[k++]=root->value;
48 }
49 }
50
51 void remove_tree(node *root){ //释放空间
52 if(root==NULL) return;
53 remove_tree(root->l);
54 remove_tree(root->r);
55 delete root;
56 }
57
58 int main(){
59 int n;
60 while(~scanf("%d",&n)){
61 for(int i=1;i<=n;i++) cin>>pre[i];
62 for(int i=1;i<=n;i++) cin>>in[i];
63 node *root;
64 int t=1;
65 buildtree(1,n,t,root); //1到n,是数字1
66 k=0; //记录结点个数
67 postorder(root);
68 for(int i=0;i<k;i++){
69 printf("%d%c",post[i],i==k-1?'\n':' ');
70 }
71 remove_tree(root);
72 }
73 return 0;
74 }

hdu1710 二叉树(C/C++)的更多相关文章

  1. hdu1710 二叉树的遍历

    Problem Description 已知前序和中序 求后序 Input The input contains several test cases. The first line of each ...

  2. 二叉树hdu1710

    学习二叉树,看了两天也不明白,唉!acm之路让我体验到要付出巨大的努力,废话不多说,看我网上找到的代码: 此题题意很明确,给你先序遍历,中序遍历,求后序遍历.但代码就让我找不到东西了. http:// ...

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

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

  4. 二叉树的前序和中序得到后序 hdu1710

    今天看学长发过来的资料上面提到了中科院机试会有一个二叉树的前序中序得到后序的题目.中科院的代码编写时间为一个小时,于是在七点整的时候我开始拍这个题目.这种类型完全没做过,只有纸质实现过,主体代码半个小 ...

  5. 如何求先序排列和后序排列——hihocoder1049+洛谷1030+HDU1710+POJ2255+UVA548【二叉树递归搜索】

    [已知先序.中序求后序排列]--字符串类型 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho在这一周遇到的问题便是:给出一棵二叉树的前序和 ...

  6. 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 ...

  7. hdu1710(二叉树的历遍)

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

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

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

  9. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  10. 二叉树的递归实现(java)

    这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...

随机推荐

  1. 【转载】synopsys中工具介绍,VCS,DC,PT等

    https://blog.csdn.net/fangxiangeng/article/details/80981536

  2. 二.navicate

    navicat -创建 -新建查询 -转储sql文件命令: 转储当前目录所有的文件与数据:mysqldump -u root db4 > db4.sql -p 转储当前目录到表结构没有数据:my ...

  3. js单线程工作

    http://www.ruanyifeng.com/blog/2014/10/event-loop.html#comment-390185

  4. ubuntu系统更换源和apt命令参数

    一:问题概述 ubuntu,我们在使用apt新装软件的时候,会使用官方的网站去下载软件,但是会因为国内的转接点太多,而导致下载的速度非常慢 ,我们可以通过换成一些中间的节点来进行下载,比如阿里源,中科 ...

  5. npm 使用阿里源

    npm config set registry https://registry.npm.taobao.org/ npm config get registry 安装vue-cli 报错 npm in ...

  6. 吴恩达老师机器学习课程chapter10——推荐算法

    吴恩达老师机器学习课程chapter10--推荐算法 本文是非计算机专业新手的自学笔记,高手勿喷. 本文仅作速查备忘之用,对应吴恩达(AndrewNg)老师的机器学期课程第十六章. 缺少数学证明,仅作 ...

  7. win10 python mysqlclient 安装问题 已解决

    用习惯了Linux   忽然换到win10 超级不习惯  今天下午就一个mysqlclient 安装弄了好长时间  ,最后发现是得改名  真是想爆粗口. 下面直接进入正题: 下载地址  https:/ ...

  8. typeScript中特殊类型定义

    // Js八种内置类型, string, number, boolean, undefined, null, object, bigint symbol // ECMAScript内置对象 Array ...

  9. Python学习笔记文件读写之遍历目录树

    随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...

  10. 自定义类型与Qt元对象系统

    个人发现一篇关于在Qt中使用元对象系统支持自定义类型的好博文,记录下防止丢失(如有侵权,烦请告知删除).博文原地址:http://qtdebug.com/qtbook-misc-qvariant/ Q ...