HDU 1710 Binary Tree Traversals(二叉树遍历)
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
思路
题意:已知前序遍历和中序遍历,求后序遍历。
#include<bits/stdc++.h> using namespace std; const int maxn = 10005; void post(int n,int a[],int b[],int c[]) { if (n <= 0) return; int pos; for (int i = 0;i < n;i++) if (b[i] == a[0]) pos = i; post(pos,a + 1,b,c); post(n - pos - 1,a + pos + 1,b + pos + 1,c + pos); c[n - 1] = a[0]; } int main() { int N; while (~scanf("%d",&N)) { int a[maxn],b[maxn],c[maxn]; for (int i = 0;i < N;i++) scanf("%d",&a[i]); for (int i = 0;i < N;i++) scanf("%d",&b[i]); post(N,a,b,c); printf("%d",c[0]); for (int i = 1;i < N;i++) printf(" %d",c[i]); printf("\n"); } return 0; }
#include<bits/stdc++.h> using namespace std; const int maxn = 1005; typedef struct Tree{ Tree *left,*right; int val; }Tree; Tree *head; Tree *build(int a[],int b[],int N) { Tree *node; for (int i = 0;i < N;i++) { if (b[i] == a[0]) { node = (Tree *)malloc(sizeof(Tree)); node->val = a[0]; node->left = build(a + 1,b,i); node->right = build(a + i + 1,b + i + 1, N - i - 1); return node; } } return NULL; } void Print(Tree *p) { if (p == NULL) return; Print(p->left); Print(p->right); if (p == head) printf("%d\n",p->val); else printf("%d ",p->val); free(p); } int main() { int N,a[maxn],b[maxn]; while (~scanf("%d",&N)) { for (int i = 0;i < N;i++) scanf("%d",&a[i]); for (int i = 0;i < N;i++) scanf("%d",&b[i]); head = build(a,b,N); Print(head); } return 0; }
HDU 1710 Binary Tree Traversals(二叉树遍历)的更多相关文章
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...
- HDU 1710 Binary Tree Traversals(二叉树)
题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 【二叉树】hdu 1710 Binary Tree Traversals
acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...
- HDU 1710 Binary Tree Traversals
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...
- hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
- 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 ...
随机推荐
- SM2国密证书合法性验证
通常我们遇到过的X509证书都是基于RSA-SHA1算法的,目前国家在大力推行国密算法,未来银行发行的IC卡也都是基于PBOC3.0支持国密算法的,因此我们来学习一下如何验证SM2国密证书的合法性.至 ...
- 纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!
前言 FineUI控件库发展至今已经有 5 个年头,目前论坛注册的QQ会员 5000 多人,捐赠用户 500 多人(捐赠用户转化率达到10%以上,在国内开源领域相信这是一个梦幻数字!也足以证明Fine ...
- promise的学习
为了解决回调地狱的问题,所以出现了promise的设计思想. promise的三种状态: pending 等待状态 resolved 完成状态 rejected 拒绝状态 promise的三种状态,只 ...
- 求height数组
procedure getheight; var i,po1,po2:longint; begin to len do begin ; po1:=i;po2:=sa[rank[i]-]; while ...
- C 语言学习的第 02 课:C 语言的开发环境
工欲善其事,必先利其器.不知道还是不是记得上一篇文章中说到的,计算机本身是一个数据输入及输出的设备.所以,为了将你大脑中的各种 idea 输入到电脑,且最终生成能够执行的程序,总是要预备点什么的. 通 ...
- 52-which 显示系统命令所在目录
显示系统命令所在目录 which command-list 参数 command-list 是which搜索的一条或多条命令(实用程序) 示例 which 单条命令 $ which ls /bin/l ...
- 【原创·总结】影响sql查询性能的因素
1.表定义 (1)如果字符串字段是经常需要用到的,可以冗余,否则不要冗余 (2)经常需要作为where的查询条件的字段,可以建索引:但是过多的索引会影响写入时的性能 (3)合理定义字段的数据类型 ( ...
- android开发------Activity生命周期
这几天工作比较忙,基本没有什么时间更新播客了. 趁着今晚有点时间,我们来简单说一下什么是Activity生命周期和它们各阶段的特征 什么是生命周期 在还没有接触android开发的时候,听到有人说Ac ...
- 使用iframe标签结合springMvc做文件上传
1.iframe.jsp <body> <h1>测试iframe文件上传</h1> <!-- 1.要求表单的target属性名称与iframe的name名字一 ...
- isinstance
class Foo: pass obj = Foo() isinstance(obj,Foo) class Foo: pass obj = Foo() isinstance(obj ,Foo) pri ...