Binary Tree Traversals
来源:互联网 发布:apache高并发解决方案 编辑:程序博客网 时间:2024/05/22 04:36
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3313 Accepted Submission(s): 1485
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.
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
91 2 4 7 3 5 8 9 64 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1
根据先序与中序求后序
#include <iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<malloc.h>using namespace std;void build(int n,int *str1,int *str2,int *str){ int k; if(n<=0) return ; else { for(int i=0;i<n;i++) if(str2[i]==str1[0]) { k=i; } build(k,str1+1,str2,str); build(n-1-k,str1+1+k,str2+1+k,str+k); str[n-1]=str1[0]; }}int main(){ int n,str1[1001],str2[1001],str[1001],i; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) scanf("%d",&str1[i]); for(i=0;i<n;i++) scanf("%d",&str2[i]); build(n,str1,str2,str); for(i=0;i<n-1;i++) printf("%d ",str[i]); printf("%d\n",str[n-1]); }}
0 0
- HDOJ1710 Binary Tree Traversals
- traversals for Binary Tree
- HDOJ1710 Binary Tree Traversals
- HDU1710:Binary Tree Traversals
- Hdu Binary Tree Traversals
- hdu Binary Tree Traversals
- Binary Tree Traversals
- hdu1710 Binary Tree Traversals
- HDU1710 Binary Tree Traversals
- hdu1710 Binary Tree Traversals
- hdu1710 Binary Tree Traversals
- hdu1710 Binary Tree Traversals
- HDU1710 Binary Tree Traversals
- HDU1710-Binary Tree Traversals
- Binary Tree Traversals
- Binary Tree Traversals
- hdu1710 Binary Tree Traversals
- HDU-Binary Tree Traversals
- android得到系统时间如何判断是白天还是晚上
- android开发中如何查看当前eclipse 是否已经安装ADT以及这个ADT的版本
- LeetCode007:WordLadder
- Windows Azure Mobiles Services实现客户端的登录注册
- ODBC连接数据库
- Binary Tree Traversals
- UVa-Getting Started
- 韩顺平PHP学习视频笔记整理006浮动窗口 表单及表单控件①
- Hibernate查询方式汇总
- Spring事务传播性与隔离级别
- 天才吧是什么???
- 15分钟学会MyEclipse导出jar文件再装换成exe可执行文件
- 是否你还会想起我
- 一个人的命运决定于晚上8点到10点之间