struct cnode
{
int info;
struct cnode *next;
struct cnode *previous;
};
typedef struct cnode cnode;
預先做好雙向鏈表:1<->2<->3<->4<->5<->6<->7
如何雙向鏈表轉換爲二進制樹
所以我試圖讓,抓住雙向鏈表的中期遞歸函數(根= 4 )並將其轉換成剩餘的二進制樹。我仍然對遞歸感到陌生,所以對代碼的解釋將非常感謝!
EX. 4
/\
2 6
/\/\
1 3 5 7
這是代碼我迄今(這是沒有太大由於與遞歸困難)
void *convert(cnode *head){
if(head == NULL)
return;
int count = 0;
cnode *tempHead = head;
while(tempHead != NULL){
count++;
tempHead = tempHead->next;
}
int move = (count/2) + (count%2);
int i;
for(i=1; i<move; i++){
head = head->next;
}
}
差不多就只有將頭指針到中間信息(4)