二叉树深度的C程序 帮忙看一下正误 本人看了好久都找不出原因
/*输入ABC##DE#G##F###回车后得不到5,是不是输入有问题。*/
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
#define ERROR 0
#include <stdio.h>
BiTree CreateBiTree(BiTree T)
{char ch;
scanf( "%c ",&ch);
if(ch== ' ') T=NULL;
else
{if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) return ERROR;
T-> data=ch;
CreateBiTree(T-> lchild);
CreateBiTree(T-> rchild);
}
return T;
}
int High(BiTree T)
{ int n,nl,nr;
if(T)
{nl=High(T-> lchild);
nr=High(T-> rchild);
if(nl> =nr)
n=nl+1;
else
n=nr+1;
}
else
n=0;
return(n);
}
main()
{BiTree T;int h;
T=CreateBiTree(T);
h=High(T);
printf( "%d ",h);
}
[解决办法]
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
#define ERROR 0
BiTree CreateBiTree()// <--------
{
BiTree T;
char ch;
ch = getch();// <--------
printf( "%c ",ch);// <--------
if(ch== ' ') T=0;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
return ERROR;
T-> data=ch;
T-> lchild = CreateBiTree();// <--------
T-> rchild = CreateBiTree();// <--------
}
return T;
}
int High(BiTree T)
{
int n,nl,nr;
if(T)
{
nl=High(T-> lchild);
nr=High(T-> rchild);
if(nl> =nr)
n=nl+1;
else
n=nr+1;
}
else
n=0;
return(n);
}
int _tmain(int argc, _TCHAR* argv[])
{
BiTree T = 0;int h = 0;
T=CreateBiTree();// <--------
h=High(T);
printf( "%d ",h);
//DeleteTree();
getch();
}