HDU (1710)Binary Tree Traversals

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
 


 

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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1003
typedef struct node
{
    int n;
    struct node *l,*r;
}BT;
BT* ct(int *f,int *s,int fl,int fr,int sl,int sr)
{
    int l,n;
     BT *p=(BT *)malloc(sizeof(BT));
      p->n=f[fl];
      if(fl==fr)
      {
          p->l=p->r=NULL;
          return p;
      }
    for(l=sl;l<=sr;l++)
        if(s[l]==f[fl])
          break;
    n=l-sl;
    if(l==sl&&sr-sl>0)  //找到的节点在第一位,且后面有节点
    {
        p->l=NULL;
        p->r=ct(f,s,fl+1,fr,l+1,sr);
        return p;
    }
    else
    if(l==sr&&sr-sl>0)//找到的节点在最后面,且前面有节点
    {
        p->r=NULL;
        p->l=ct(f,s,fl+1,fr,sl,sr-1);
        return p;
    }
   p->l=ct(f,s,fl+1,fl+n,sl,l-1);//这里可能还可以优化
   p->r=ct(f,s,fl+n+1,fr,l+1,sr);
   return p;

}
void disp(BT *p,int c)
{
    if(p->l!=NULL)
        disp(p->l,2);
    if(p->r!=NULL)
        disp(p->r,2);
    if(c!=1)
     printf("%d ",p->n);
     else
     printf("%d\n",p->n);//第一次是忘记加换行符了,PE了一次,唉!
}
int main()
{
  int i,n,f[N],s[N];
  BT* p=NULL;
  while(scanf("%d",&n)!=EOF)
  {
       for(i=0;i<n;i++)
        scanf("%d",&f[i]);//输入前序
       for(i=0;i<n;i++)
        scanf("%d",&s[i]);//输入后序
       p=ct(f,s,0,n-1,0,n-1);//还原这课树
        disp(p,1);//后序遍历
  }

  return 0;
}
#include <stdio.h>//在上面的代码提交了几秒之后,我突发奇想,改成了以下代码
#include <string.h>//想到书上说的不用递归的遍历,用栈来模仿、
#include <stdlib.h>//还好。成功AC了,上面的代码是31MS,这个代码是15MS,而且更短,更节省内存
#define N 1003         //不知道0Ms的代码怎么样的,佩服!
void
 ct(int *f,int *s,int fl,int fr,int sl,int sr,int c)
{

    int
 l,n;
      if
(fl==fr)
      {
   if(c!=1)
           printf("%d ",f[fl]);
           else

            printf("%d\n",f[fl]);
           return
 ;
      }

    for
(l=sl;l<=sr;l++)
        if
(s[l]==f[fl])
          break
;
    n=l-sl;
    if
(l==sl&&sr-sl>0)
    {

        ct(f,s,fl+1,fr,l+1,sr,2);
         if
(c!=1)
           printf("%d ",f[fl]);
           else

            printf("%d\n",f[fl]);
         return
 ;
    }

    else
    if
(l==sr&&sr-sl>0)
    {

        ct(f,s,fl+1,fr,sl,sr-1,2);
        if
(c!=1)
           printf("%d ",f[fl]);
           else

            printf("%d\n",f[fl]);
         return
 ;
    }

   ct(f,s,fl+1,fl+n,sl,l-1,2);
   ct(f,s,fl+n+1,fr,l+1,sr,2);
    if
(c!=1)
        printf("%d ",f[fl]);
    else

       printf("%d\n",f[fl]);
    return
 ;
}

int
 main()
{

  int
 i,n,f[N],s[N];
  while
(scanf("%d",&n)!=EOF)
  {

       for
(i=0;i<n;i++)
        scanf("%d",&f[i]);
       for
(i=0;i<n;i++)
        scanf("%d",&s[i]);
       ct(f,s,0,n-1,0,n-1,1);
  }


  return
 0;
}

 

                                                                 -----江财小子

你可能感兴趣的:(binary)