主题:二叉树遍历问题
我的二叉树在输出时只能输出一个根节点,请高手指点!!!
#include "stdio.h"
#include "stdlib.h"
struct tree
{
int data;
tree *left,*right;
};
tree *creat(tree *t)
{
int a;
scanf("%d",&a);
if(a==-2)
t=NULL;
else
{
t=(tree *)malloc(sizeof(tree));
t->data=a;
printf("a ");
creat(t->left);
printf("b ");
creat(t->right);
}
return t;
}
void display(tree *t)
{
if(t!=NULL)
{
printf("%d ",t->data);
display(t->left);
display(t->right);
}
}
void main()
{
tree *root;
root=creat(root);
display(root);
}
输入 2 3 -2 5 -2 -2 -2
只能输入一个2 然后报错
#include "stdio.h"
#include "stdlib.h"
struct tree
{
int data;
tree *left,*right;
};
tree *creat(tree *t)
{
int a;
scanf("%d",&a);
if(a==-2)
t=NULL;
else
{
t=(tree *)malloc(sizeof(tree));
t->data=a;
printf("a ");
creat(t->left);
printf("b ");
creat(t->right);
}
return t;
}
void display(tree *t)
{
if(t!=NULL)
{
printf("%d ",t->data);
display(t->left);
display(t->right);
}
}
void main()
{
tree *root;
root=creat(root);
display(root);
}
输入 2 3 -2 5 -2 -2 -2
只能输入一个2 然后报错