看看这个C语言写的树的最简单程序哪有问题

来源:百度知道 编辑:UC知道 时间:2024/05/07 10:07:21
#include <stdio.h>
#include <malloc.h>

typedef struct node{

node *lchild,*rchild;
int data;

}NODE;

NODE * CreatBTpre ()
{
NODE *T;
char ch;

scanf("%c",&ch);

if(ch == '#' ) T=NULL;

else

{

T = ( NODE * ) malloc ( sizeof( NODE ));

T->data = ch;

T->lchild = CreatBTpre();

T-> rchild = CreatBTpre();

return T;

}
}

NODE* LDR(NODE *root)
{
if(root)
{
root = LDR (root->lchild);

printf("%c ",root->data);

root = LDR (root -> rchild);
}

return (root);
}

void main()
{

NODE *T;

T=CreatBTpre();

LDR(T);

}

为什么输出不了?
请看一下有什么问题..

#include <stdio.h>
#include <malloc.h>

typedef struct node{

node *lchild,*rchild;
int data;

}NODE;

NODE * CreatBTpre ()
{
NODE *T;
char ch;

scanf("%c",&ch);
getchar();//多余的回车会影响输入

if(ch == '#' ) T=NULL;

else

{

T = ( NODE * ) malloc ( sizeof( NODE ));

T->data = ch;

T->lchild = CreatBTpre();

T-> rchild = CreatBTpre();

}
return T;
}

NODE* LDR(NODE *root)
{
if(root != NULL)
{
LDR (root->lchild); //不应该改变root

printf("%c ",root->data);

LDR (root -> rchild);
}

return (root);
}

void main()
{

NODE *T;

T=CreatBTpre();
LDR(T);

}