注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Minary_Acdream

http://f10.moe/

 
 
 

日志

 
 

[hdu] 1710 Binary Tree Traversals  

2012-10-10 09:06:53|  分类: HDU |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
= =花了两天的时间把树的基础给过了一遍,现在是练习阶段。
       最基础的一道题目,告诉你先序序列和中序序列,输出后序序列。

Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1589    Accepted Submission(s): 735


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.
[hdu] 1710 Binary Tree Traversals - Minary - Minary_Acdream
 

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
 

Source
 

Recommend
lcy



#include<iostream>
using namespace std;

typedef struct Bnode
{
int data;
struct Bnode *Rson,*Lson;
}Bnode,*Bptr;

int flag;
int c[1001];
Bptr creatB(int *a,int *b,int n)
{
Bptr s;
int *p,k;
if(n <= 0) return NULL;
s = new Bnode;
s->data = *a;
for(p = b;p < b + n;p ++)
{
if(*p == *a) break;
}
k = p - b;
s->Lson = creatB(a + 1,b,k);
s->Rson = creatB(a + k + 1,p + 1,n - k - 1);
return s;
}

void PostOrder(Bptr b)
{
if(b != NULL)
{

PostOrder(b->Lson);
PostOrder(b->Rson);
if(flag)cout<<" ";
flag = 1;
cout<<b->data;
}

}

int main()
{
int a[1001],b[1001];
int n;
while(cin>>n)
{
flag = 0;
memset(c,0,sizeof(c));
for(int i = 0;i < n;i ++) scanf("%d",&a[i]);
for(int i = 0;i < n;i ++) scanf("%d",&b[i]);
PostOrder(creatB(a,b,n));
cout<<endl;
}
}
/*
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*/




  评论这张
 
阅读(326)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018