BinaryTree* sortedListToBST(ListNode *& list, int start, int end) {
if (start > end) return NULL;
// same as (start+end)/2, avoids overflow
int mid = start + (end - start)/2;
BinaryTree *leftChild = sortedListToBST(list, start, mid-1);
BinaryTree *parent = new BinaryTree(list->data);
parent->left = leftChild;
list = list->next;
parent->right = sortedListToBST(list, mid+1, end);
return parent;
}
BinaryTree* sortedListToBST(ListNode *head, int n) {
return sortedListToBST(head, 0, n-1);
}
這是一個將排序後的列表傳遞給BST的函數。 我在第一行不理解。爲什麼「ListNode *&
」...如果只是「ListNode*
」爲什麼錯了? 感謝您的任何解釋。關於C++的語法問題
謝謝。還有一個問題。如果只是「ListNode &列表」那一定是錯的,我們爲什麼需要「*」也..的C++愛好者原諒我,我愚蠢的問題