Online Judge Online Exercise Online Teaching Online Contests Exercise Author
F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
C/C++/Java Exams
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests
DIY | Web-DIY beta
Recent Contests
Author dddj123
Mail Mail 0(0)
Control Panel Control Panel
Sign Out Sign Out
(点击查看详情)杭电ACM-LCY算法培训“进阶班”
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19243 Accepted Submission(s): 7899
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
已知先序和中序,求后序
#include<iostream>
using namespace std;
int pre[10007],in[10007];//前序和中序数组
int cnt = 1;//保持输出格式正确
int n;
void getTreeLRN( int a[],int fa,int la, int b[],int fb,int lb )//fa,la,fb,lb,都表示下标
{
if( fa>la )
return ;
int k=fb;
while(b[k]!=a[fa] )//寻找根节点
k++;
getTreeLRN( a,fa+1,k-fb+fa , b,fb , k-1);//左子树
getTreeLRN( a,k-fb+fa+1 ,la, b,k+1,lb);//右子树
if(cnt!=n)
cout << a[fa] << " ", cnt++;
else
cout << a[fa] << endl;
}
int main()
{
freopen("11.txt", "r", stdin);
while(scanf("%d",&n)!=EOF)
{
cnt = 1;
for (int i = 0; i < n; i++)
scanf("%d", &pre[i]);
for (int i = 0; i < n; i++)
scanf("%d", &in[i]);
getTreeLRN(pre, 0, n - 1, in, 0, n - 1);
}
return 0;
}
附加一个已知中序和后序,求先序
#include<iostream>
using namespace std;
//已知中序和后序,求先序
const int maxn = 10007;
int pre[maxn],in[maxn],post[maxn];//前序和中序数组
int cnt = 1;//保持输出格式正确
int n;
void getTreeinorder( int a[],int fa,int la, int b[],int fb,int lb )//fa,la,fb,lb,都表示下标 a[]表示后序,b表示中序
{
if( fa>la)
return ;
int k=fb;
while(a[la]!=b[k] )//寻找根节点
k++;
cout << a[la] << " ";
getTreeinorder( a,fa,fa+k-1-fb , b,fb , k-1);//左子树
getTreeinorder( a,fa+k-fb , la-1, b,k+1,lb);//右子树
}
int main()
{
freopen("11.txt", "r", stdin);
while(scanf("%d",&n)!=EOF)
{
cnt = 1;
for (int i = 0; i < n; i++)
scanf("%d", &post[i]);
for (int i = 0; i < n; i++)
scanf("%d", &in[i]);
getTreeinorder(post, 0, n - 1, in, 0, n - 1);
}
return 0;
}