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. ES6-Promise上

    一.Promise作用:解决回调地狱问题 transitionend是过渡结束事件,只要过渡结束就会触发: 回调地狱:指的是层层嵌套的回调函数,代码看起来非常晕 <!DOCTYPE html&g ...

  2. SAP 附件功能 PRD环境无法删除 VIEW_ATTA

    如图:界面上面没有打勾确认按钮 解决方案:来源网址 How to disable, delete and edit buttons function in attachment list. | SAP ...

  3. 13-之容器资源需求、资源限制及Metric-server(Heapster)

    目录 容器资源需求.资源限制及Heapster Heapster 资源指标API及自定义指标API k8s-promtheus监控部署 node-exporter prometheus kube-st ...

  4. 计算机科学导论-第三版-学习笔记-chapter2-数字系统

    原本看答案的网站被上保护了,我没账号看不了,开摆. 猜测是那边的学生做作业用chatGPT,部分教师觉得不行,禁止使用的同时把答案都上锁了. 也可能是单纯因为我没报课就没账号. 复习题 1.定义一个数 ...

  5. The 17th Zhejiang Provincial Collegiate Programming Contest B.Bin Packing Problem

    题意 给定n个物品,和一个容量为C的桶 需要求出为了装下这些物品,分别使用首次适应算法(FF).最佳适应算法(BF)需要的桶的数量 \(n \leq 10^6\) 思路 BF:容易想到可以用set维护 ...

  6. 监听异常:The listener supports no services

    数据库版本:单机环境19c 实例是正常的 [oracle@sit19c admin]$ sqlplus / as sysdba SQL*Plus: Release 19.0.0.0.0 - Produ ...

  7. k8s pod 抓包

    首先安装tcpdump: yum install tcpdump kubectl get pod -o wide查看pod在哪个节点上 docker ps 查看container的id 查看pid: ...

  8. unity VideoPlayer 视频静音

    standVideo.SetDirectAudioMute(0,true);

  9. vim重复、删除、复制、粘贴命令

    0.选中 V+(上.下键)    表示选中 1.删除 1.输入10x,删除10个连续字符 2.输入3dd,将会删除3行文本 在普通模式下,你还可以使用dw或者daw(delete a word)删除一 ...

  10. Java8函数式编程(A)

    将行为作为数据传递 函数编程的最直接的表现,莫过于将函数作为数据自由传递,结合泛型推导能力,使代码表达能力获得飞一般的提升. Java8怎么支持函数式编程? 主要有三个核心概念: 函数接口(Funct ...