用先序递归建树,函数无法退出

ruibinwilson
  • 6
// Tree struct
typedef struct tree
{
    ElemType data;
    struct tree * lchild;
    struct tree * rchild;
}TreeNode,* Tree;

void CreateTree(Tree* t)
{
    char ch;
    scanf("%c",&ch);
    if ( ch == '#' ){
        *t = NULL;
    }
    else
    {
        *t = (Tree)malloc(sizeof(TreeNode));
        if ( !(*t) )
        {
            printf("memory allocate error!");
            return ;
        }
        (*t)->data = ch;
        CreateTree(&((*t)->lchild));
        CreateTree(&((*t)->rchild));
    }
    return ;
}
int main()
{
    Tree T;
    printf("\nPlease input node in preorder way,'#'means NULL:");
    CreateTree(T);
}

程序执行到CreateTree里面无法退出,递归退出判断条件如何写?

回复
阅读 1.3k
宣传栏