【发布时间】:2014-03-25 04:52:58
【问题描述】:
在这段代码中,我正在创建一个二叉树,直到用户想要节点数。 但是在从用户那里获取输入时,它在某处失败了..
struct node *createTree(struct node *root)
{
int n;
char ch;
struct node *t1;
t1=(struct node *)malloc(sizeof(struct node));
printf("Enter Element\n");
scanf("%d",&n);
if(root==NULL)
{
t1->data=n;
t1->left=NULL;
t1->right=NULL;
root=t1;
}
printf("do you want to add node at left?\n");
这不正常
scanf("%c",&ch);
if(ch=='y')
{
t1->left=createTree(t1->left);
}
printf("do you want to add node at right?\n");
scanf("%c",&ch);
if(ch=='y')
{
t1->right=createTree(t1->right);
}
return root;
}
【问题讨论】: