C 将有序链表转换为二叉搜索树

C 将有序链表转换为二叉搜索树,c,linked-list,binary-search-tree,C,Linked List,Binary Search Tree,这是我的密码。请让我知道错误。问题是如何将给定的有序链表转换为二叉搜索树 谢谢 struct treenode* makeBSTnode(struct node* head) { struct treenode *root=(struct treenode*)malloc(sizeof(struct treenode)); root->data = head->data; root->leftchild = NULL; root->righ

这是我的密码。请让我知道错误。问题是如何将给定的有序链表转换为二叉搜索树

谢谢

struct treenode* makeBSTnode(struct node* head)
{
    struct treenode *root=(struct treenode*)malloc(sizeof(struct treenode));
    root->data = head->data;
    root->leftchild = NULL;
    root->rightchild = NULL;
    return root;
}


struct treenode* makeBST(struct node **head,int iterations)
{
    if(iterations==0)
    {
        /*this is the code for the base condition*/    
        struct treenode *root=(struct treenode*)malloc(sizeof(struct treenode));
        root = makeBSTnode(*head);
        return root;
    }
    else
    {
        /*this is for the case when the node is not single but should be broken down into parts*/
        struct node *leftchildnode=NULL,*rightchildnode=NULL,*rootnode=NULL,*temp;
        temp = (struct node*)malloc(sizeof(struct node));
        struct treenode *root = (struct treenode*)malloc(sizeof(struct treenode)); 
        int limit = (int)ceil((float)(3*iterations)/2);
        for(int i=1;i<=limit;i++)
        {
            if(i==(int)ceil((float)iterations/2))
            {
                leftchildnode = (struct node*)malloc(sizeof(struct node));
                leftchildnode = *head;
            }
            else if(i==iterations)
            {
                rootnode = (struct node*)malloc(sizeof(struct node));
                rootnode = *head;
            }
            else if(i==(int)ceil((float)(3*iterations)/2))
            {
                rightchildnode = (struct node*)malloc(sizeof(struct node));
                rightchildnode = *head;
            }
            if(*head)
            (*head) = (*head)->next;
            else
            break;
        }
        root = makeBSTnode(rootnode);
        root->leftchild = makeBST(&temp,(int)ceil((float)iterations/2));
        root->rightchild = makeBST(&rootnode,(int)ceil((float)(3*iterations/2)));
        return root;
    }
}
我在main中将此makeBST函数调用为:
根=makeBST和head,intceilfloatlength/2

这是返回无效的树还是崩溃?错误报告?你试过调试吗?@dckuehn-程序在*head=*head->next点中断@放松-你能再详细一点吗?因为程序不会因为malloc而中断或崩溃。@Aspirant9 tl;dr表示不需要强制转换,通过强制转换,您可能隐藏了许多逻辑错误。