qq_34378013 2016-05-02 12:56 采纳率: 14.3%
浏览 1439

C语言二叉树遍历的问题

 #include<stdio.h>
#include<stdlib.h>


typedef struct node
{
    int num;
    struct node *l;
    struct node *r;
}LN;



void Creat(LN *L);
void Change(LN *L);
int Print(int n);




void main()
{
    LN *L;
    int n;
ii:
    printf("Press 0 to end: \n");
    printf("Press 1 to Creat a Linklist: \n");
    printf("Press 2 to Change the child: \n");
    scanf("%d",&n);
    switch(n)
    {
        case 0:
            system("CLS");
            goto cc;
        case 1:
            system("CLS");
            printf("Input the root number: \n");
            Creat(L);
            system("CLS");
            break;
        case 2:
            Change(L);
            break;
    }
    goto ii;
cc:;

}


void Creat(LN *L)
{
    int n;
    scanf("%d",&n);
    if(n==0)
    {
        L=NULL;
    }
    else
    {
        L=(LN*)malloc(sizeof(LN));
        L->num=n;
        printf("Creat %d's leftchlid: \n",n);
        Creat(L->l);
        printf("Creat %d's rightchild: \n",n);
        Creat(L->r);
    }
}




void Change(LN *L)
{
    if (L!=NULL)
    {
        printf("%d",L->num);
        Change(L->l);
        Change(L->r);
    }
}

我的Change函数是先序遍历,小弟刚学实在不知道错在哪里。怎么从先序遍历算法中交换左右孩子,谢谢大神门!!!!!
还有怎么按层次遍历,感觉按层次没有办法用递归算法
如果我要想从大到小的顺序输出二叉排序树的各结点的算法,怎么实现,跪求大神门的指教,感谢!

  • 写回答

1条回答

  • devmiao 2016-05-02 15:54
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler