一.逐个字符的建立
void CreateBiTree(BTNode *&T)
{
char c;
scanf("%c",&c);
if(c=='#')
T = NULL;
else
{
T = (BTNode *)malloc(sizeof(BTNode));
T->data = c;
CreateBiTree(T->left);
CreateBiTree(T->right);
}
}
二.将输入存储到数组建立
(-1表示NULL)
int data[] = {1,2,3,-1,-1,5,-1,-1,4,-1,-1};
int i = 0;
void CreateBiTree(BTNode *&T)
{
if(data[i++]==-1)
T = NULL;
else
{
T = (BTNode *)malloc(sizeof(BTNode));
T->data = data[i-1];
CreateBiTree(T->left);
CreateBiTree(T->right);
}
}
网友评论