您的位置:首页 > 其它

HDU-1710-Binary Tree Traversals

2013-08-01 11:44 281 查看
/*

Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2501    Accepted Submission(s): 1088


[align=left]Problem Description[/align]
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.



 

 

[align=left]Input[/align]
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.

 

 

[align=left]Output[/align]
For each test case print a single line specifying the corresponding postorder sequence.

 

 

[align=left]Sample Input[/align]

9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6

 

 

[align=left]Sample Output[/align]

7 4 2 8 9 5 6 3 1

 

 

[align=left]Source[/align]
HDU 2007-Spring Programming Contest
 

 

[align=left]Recommend[/align]
lcy
 

 

本题如果学过数据结构的话,应该问题不大,就是给一个先序遍历,一个中序遍历,

求后序遍历,一般我们做的时候就是根据先序遍历和中序遍历建造一个树,然后再

对这个树进行后序遍历输出就行了。后序遍历树很简单,我们就不多说了,关键是

怎样建树。首先我们应该搞清楚先序遍历和中序遍历,在中序遍历中如果这个数出

现在某个节点的前面就表明此数在这个节点的左边,如果这个数出现在某个节点的

后面就表明此数出现在这个节点的右边。*/

代码:

 

 

 

#include<stdio.h>//78MS 1416K
#include<stdlib.h>
int n,pre[1005],in[1005];
typedef struct node
{
int data;
int index;
struct node *Lchild,*Rchild;
}bitree,*Tree;

void dfs(Tree &root,int j)
{
if(root==NULL)
{
root=(Tree)malloc(sizeof(bitree));//空就创建
root->data=in[j];
root->index=j;
root->Lchild=NULL;
root->Rchild=NULL;
return ;   //跳出!!!
}
else
{
if(j<root->index)//节点存在时,
dfs(root->Lchild,j);
else
dfs(root->Rchild,j);

}
}
void createbitree(Tree &root)
{
int i,j,index;
root=(Tree)malloc(sizeof(bitree));
for(i=1;i<=n;i++)
if(in[i]==pre[1])
{
root->data=pre[1];
root->index=i;
root->Lchild=NULL;
root->Rchild=NULL;
break;
}
index=i;  //4
for(i=2;i<=n;i++)//前序
for(j=1;j<=n;j++)//中序 查找,并创建树结构
if(in[j]==pre[i])
{
if(j<index)//4
dfs(root->Lchild,j);
else
dfs(root->Rchild,j);
break;
}
}
void post(Tree root,int x)//后序遍历
{
if(root==NULL)
return ;
post(root->Lchild,x+1);

post(root->Rchild,x+1);//递归

if(x==0)
printf("%d",root->data); //标记与根节点区别开来

else
printf("%d ",root->data);
}
int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
Tree root;
for(i=1;i<=n;i++)
scanf("%d",&pre[i]);

for(i=1;i<=n;i++)
scanf("%d",&in[i]);
createbitree(root);
post(root,0);
printf("\n");
}
return 0;
}


//比较牛的代码

//递归的时候模拟一个栈,把树按根,右子树,左子树存入,再LIFO输出

 

#include<stdio.h>//46MS 160K
#define M 1001
int stack[M];
int top;
void Create(int *pre,int pre_len,int *in,int in_len)//pre[], n, in[], n (n节点数,)
{
int index;
if(pre_len)
{
stack[top++]=pre[0];
for(index=0;in[index]!=pre[0];index++);//在中序里,找不是总的根节点的节点,index,在中序中的位置
Create(pre+index+1,pre_len-index-1,in+index+1,in_len-index-1);

Create(pre+1,index,in,index);
}
}
int main()
{
int n,i;
int pre[M],in[M];
while(scanf("%d",&n)!=-1){
for(i=0;i<n;i++)
scanf("%d",pre+i);//相当于pre[i];

for(i=0;i<n;i++)
scanf("%d",in+i);
top=0;
Create(pre,n,in,n);
while(top--)
{
if(top==0){
printf("%d\n",stack[top]);
break;
}
printf("%d ",stack[top]);
}
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: