桃灯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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog