【发布时间】:2015-05-07 17:13:15
【问题描述】:
我正在尝试使用 C 将整数数组转换为二叉树。
例如对于数组a[10]={5,2,1,6,7,3,4},二叉树应该是这样的
5
/ \
2 1
/\ /\
6 7 3 4
我尝试使用以下代码进行转换
typedef struct btree
{
int value;
struct btree *left;
struct btree *right;
}Btree;
void insert(Btree *t,int *a,int index,int n)
{
t=(Btree *)malloc(sizeof(Btree));
if(index<n)
{
t->value=a[index];
insert(t->left,a,2*index,n);
insert(t->right,a,2*index+1,n);
}
}
int main(void) {
int a[100],i;
Btree *t;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
insert(t,a,0,i+1);
return 0;
}
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: c arrays binary-tree