桃灯czh 2016-05-09 14:04 采纳率: 12.5%
浏览 1177

关于二叉树的创建与遍历 请问哪里有问题我的代码

先输入一个字符串 然后求先中后序遍历
http://paste.ubuntu.org.cn/4213378

#include
#include
char w[100];
struct node
{
char data;
struct node *l;
struct node *r;
};
void creat(struct node *&T,char *w)
{
char ch;int p;
ch=*w;
if(ch=='\0') p=1;
if(ch==',') {T=NULL;w++;}
else
{
T=(struct node *)malloc(sizeof(struct node));
T->data=ch;
w++;
// printf("%c",T->data);
creat(T->l,w);
creat(T->r,w);
}

}
void travel(struct node *T)
{
if(T)
{
travel(T->l);
printf("%c",T->data);
travel(T->r);
}

}
void travel2(struct node *T)
{
if(T)
{
travel2(T->l);
travel2(T->r);
printf("%c",T->data);
}

}
void travel3(struct node *T)
{
if(T)
{
printf("%c",T->data);
travel3(T->l);
travel3(T->r);

}

}
int main()
{
struct node *head,*T;
scanf("%s",w);
T=NULL;
creat(T,w);
// printf("%c",head->r->data);
travel3(T);
printf("\n");
travel(T);
printf("\n");
travel2(T);
printf("\n");

}

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-05-09 14:08
    关注

    遍历没有什么问题,关键是creat,不理解为什么你已经有了全局变量w,还要搞一个参数变量也叫w,思路混乱,不是自己找麻烦么。

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿