改成这样就好了,但为什么不能用单指针,一定要用双指针呢?#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
typedef struct BiTNode *LinkTree;
void CreateBiTree(BiTree *T) //此处输入空格代表空,默认按前序遍历完全二叉树的方式输入数据
{
char c;
scanf("%c",&c);
if(' ' == c)
{
(*T) = NULL;
}
else
{
(*T) = (LinkTree)malloc(sizeof(BiTNode));
(*T)->data = c;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
void PreOrderTraverse(BiTree T,int level)
{
if(T)
{
printf("字符 %c 在第 %d 层\n",T->data,level);
PreOrderTraverse(T->lchild,level+1);
PreOrderTraverse(T->rchild,level+1);
}
}
int main()
{
int level = 1;
BiTree T = NULL;
CreateBiTree(&T);
PreOrderTraverse(T,level);
return 0;
}
|