A grammar question about c++ -
binarytree* sortedlisttobst(listnode *& list, int start, int end) { if (start > end) return null; // same (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); }
this function transferring sorted list bst.. don't understand in 1st line. why "listnode *&
"... if "listnode*
" why wrong? thank explanation.
thanks. 1 more question.. if "listnode &list" must wrong, why need "*" also,.. amateur of c++ forgive me stupid question
this reference pointer. changes make listnode (making point @ else) affect variable passed function.
if remove this, recursive calls, e.g. binarytree *leftchild = sortedlisttobst(list, start, mid-1);
, modify copy of list
instead of actual list
, , code behave differently.
Comments
Post a Comment