HDU-1710-Binary Tree Traversals

/*

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


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
 


 

Source
 


 

Recommend
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;
} 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值