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. JAVA框架知识

    Java中的MVC: M是指模型层,C则是控制器,V是指视图:一个完整的请求过程是,客户端发送请求到控制器,控制器调用业务层处理请求,并返回处理结果给视图,其中业务层是调用Dao层去完成业务逻辑的:M ...

  2. gitee上传VS2022已有项目

    1.在gitee上新建仓库: 2.复制新建仓库地址: 3.用VS2022打开先有项目,找到Git更改项: 4.点击创建Git存储库: 5.创建本地仓库并推送到远程,点击创建并推送: 6.等待创建成功即 ...

  3. Flutter配置签名打包全流程填坑笔记

    1.配置包名和版本 找到android-app-src-build.gradle文件在defaultConfig{...}中配置好版本号以及包名 2.生成key 将keytool路径添加进环境变量,c ...

  4. 记一个在线工具网站,程序员必备,json格式化、压缩、转义,加解密 编码解码

    简用-在线工具箱-简单易用-工具大全 提供 json格式化,json代码压缩,json校验解析,json数组解析,json转xml,xml转json,json解析,json在线解析,json在线解析及 ...

  5. httpclint的传值和访问https

    一.StringContent与FormUrlEncodedContent 可参考这篇文章写的非常好: https://blog.csdn.net/lxrj2008/article/details/7 ...

  6. Jupyter Notebook安装代码提示功能

    默认Jupyter Notebook没有安装代码提示功能,但是我们可以可通过如下命令安装和配置使得Jupyter Notebook具备代码提供功能. (确保Anaconda在环境变量里)1.电脑上搜索 ...

  7. nop4.3 用户权限管理

    权限管理涉及到5张表: //用户表 select * from Customer //角色表select * from CustomerRole //用户和角色对应关系select * from Cu ...

  8. 在LUbuntu上搭建Neovim+LaTeX环境

    目录 安装.配置vimtex 安装texlive 安装zathura 安装.配置vimtex Plug 'lervag/vimtex' let g:tex_flavor= 'latex' " ...

  9. maven加载本地的jar包

    方式1 ,通过scope = system的方式加载 <dependency> <groupId>com.sun.jna</groupId> <artifac ...

  10. flex height变高了

    在做移动端项目时,使用了flex布局后,所有的子项高度变成了一致 问题:在flex布局中,如何保持子项自身高度 原因: Flex 布局会默认: 把所有子项变成水平排列.默认不自动换行.让子项与其内容等 ...