具有n个节点的非空完全二叉树采用顺序存储结构,节点信息存放于数组a[0....n-1]中,请设计

2025-01-02 14:51:28
推荐回答(1个)
回答1:

BiTree * BuildByFirst(int A[], int cur, int n)
{
if (cur >= n)
return NULL;
BiTree *tmp = new BiTree()
tmp->data = A[cur];
tmp->left = BuildByFirst(A, 2 * cur + 1, n);
tmp->right = BuildByFirst(A, 2 * cur + 2, n);
return tmp;
}
void Conversion(int A[],int n, BiTree &T)
{
BiTree *tmp = BuildByFirst(A, 0, n);
T = *tmp;
delete tmp;
}