二叉树的建立和遍历

#include<stdio.h>
#include<stdlib.h>
typedef  struct  tree
{
    int  num;
    struct  tree *lchild,*rchild;
}tree;
tree *creatree()
{
    int num;
    tree *T;
    scanf("%d",&num);
    if(num==0)
      T=NULL;
      else
      {
          T=(tree*)malloc(sizeof(tree));
          T->num=num;
          T->lchild=creatree();
          T->rchild=creatree();
      }
      return  T;
}
void  visit(tree *T)
{
   if(T!=NULL)
   {
       printf("%d\t",T->num);
       visit(T->lchild);
       visit(T->rchild);
   }
}
int  main()
{
     tree *T;
     T=creatree();
     visit(T);
     puts(" ");
    // system("pause");
     return  0;
}   

posted @ 2011-08-11 17:06  bcy  阅读(210)  评论(0编辑  收藏  举报