二叉树一棵(先,中,后序遍历)

      好久没来这里了,上次写的东西都不晓得放了多久了,想必快要腐烂了,还是来添点其他的东西吧,想来想去还是搞点绿化好,种一棵叔,希望这棵小树能茁壮成长,将来为祖国抵御沙尘暴。

#include   "stdio.h"
#include   "conio.h"
#include   "string.h"
#define   Null   0
struct   btnode
{
    char   c;
    struct   btnode   *lchild,*rchild;
};
struct   btnode   *creatbt()                 /*用递归创建*/
{
    struct   btnode   *root;

    root=(struct   btnode*)malloc(sizeof(struct   btnode));
    scanf("%c",&root->c);
    if(root->c=='#')
    root=Null;
    else   {


                root->lchild=creatbt();
                root->rchild=creatbt();
              }
    return   root;
}
void   preorder(struct   btnode   *root)     /*先序遍历二叉树   */
{
    if(root!=Null)
    {
      printf("%c",root->c);
      preorder(root->lchild);
      preorder(root->rchild);
    }
}
void   inorder(struct   btnode   *root)       /*中序遍历二叉树*/
{
    if(root!=Null)
    {
      inorder(root->lchild);
      printf("%c",root->c);
      inorder(root->rchild);
    }
}
void   postorder(struct   btnode   *root)     /*后序遍历*/
{
    if(root!=Null)
    {
      postorder(root->lchild);
      postorder(root->rchild);
      printf("%c",root->c);
    }
}
int   main(void)
{
    struct   btnode   *Root;
    Root=creatbt();
    printf("/n");
    preorder(Root);
    printf("/n");
    inorder(Root);
    printf("/n");
    postorder(Root);
    printf("/n");
    getch();
    return   0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值