Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想使用递归创建一个二叉树。我下面的代码有什么问题? #包括 #包括 结构体{ 结构bt*左; int数据; 结构bt*对; }; 结构bt*root,*p1=NULL; 结构bt*创建_bt(结构bt*); main(){ p1=创建_bt(根); printf(“创建的二叉树”); } 结构bt*创建(结构bt*根){ INTA; printf(“输入数据:”); scanf(“%d”和“&a”); 如果(a==-1){ root=NULL; } 否则{ root=(struct bt*)malloc(sizeof(struct bt)); 根->数据=a; 根->左=创建(根->左); 根->右=创建(根->右); printf(“%d\n”,根->数据); p1=根; } 返回p1; }_C_Tree - Fatal编程技术网

我想使用递归创建一个二叉树。我下面的代码有什么问题? #包括 #包括 结构体{ 结构bt*左; int数据; 结构bt*对; }; 结构bt*root,*p1=NULL; 结构bt*创建_bt(结构bt*); main(){ p1=创建_bt(根); printf(“创建的二叉树”); } 结构bt*创建(结构bt*根){ INTA; printf(“输入数据:”); scanf(“%d”和“&a”); 如果(a==-1){ root=NULL; } 否则{ root=(struct bt*)malloc(sizeof(struct bt)); 根->数据=a; 根->左=创建(根->左); 根->右=创建(根->右); printf(“%d\n”,根->数据); p1=根; } 返回p1; }

我想使用递归创建一个二叉树。我下面的代码有什么问题? #包括 #包括 结构体{ 结构bt*左; int数据; 结构bt*对; }; 结构bt*root,*p1=NULL; 结构bt*创建_bt(结构bt*); main(){ p1=创建_bt(根); printf(“创建的二叉树”); } 结构bt*创建(结构bt*根){ INTA; printf(“输入数据:”); scanf(“%d”和“&a”); 如果(a==-1){ root=NULL; } 否则{ root=(struct bt*)malloc(sizeof(struct bt)); 根->数据=a; 根->左=创建(根->左); 根->右=创建(根->右); printf(“%d\n”,根->数据); p1=根; } 返回p1; },c,tree,C,Tree,声明: p1=root应该在else语句之外,因此如果a==-1则返回NULL,左/右子级标记为NULL。 除此之外,您的代码在构建二叉树时似乎是正确的。 希望这能有所帮助。是什么让你觉得有问题?也就是说,你的错误是什么?这是编译器错误、链接错误还是运行时错误?您收到了什么诊断信息?请查看并更新您的问题。告诉我们错误,我们将提供帮助。(或一些测试输入/输出) #include<stdio.h> #include<stdlib.h> struct bt{ struct

声明:
p1=root应该在else语句之外,因此如果
a==-1
则返回NULL,左/右子级标记为NULL。 除此之外,您的代码在构建二叉树时似乎是正确的。
希望这能有所帮助。

是什么让你觉得有问题?也就是说,你的错误是什么?这是编译器错误、链接错误还是运行时错误?您收到了什么诊断信息?请查看并更新您的问题。告诉我们错误,我们将提供帮助。(或一些测试输入/输出)
#include<stdio.h>
#include<stdlib.h>

struct bt{

struct bt *left;
int data;
struct bt *right;
};

struct bt *root,*p1 = NULL;

struct bt* create_bt(struct bt*);

main(){

p1 = create_bt(root);
printf("Binary tree created\n");
}

struct bt* create_bt(struct bt *root){

int a;
printf("Enter data:");
scanf("%d",&a);
if(a == -1){
    root = NULL;
        }
else{
    root = (struct bt*)malloc(sizeof(struct bt));
    root -> data = a;
    root -> left = create_bt(root -> left);
    root -> right = create_bt(root -> right);
    printf("%d\n",root -> data);
    p1 = root;
    }
return p1;
}