我正在查看以下算法来展平“Programming Interviews Exposed”一书中的树状链表系统:
void flattenList( Node *head, Node **tail)
Node *curNode = head;
while( curNode ){
/* The current node has a child */
if( curNode->child ){
append( curNode->child, tail );
}
curNode = curNode->next;
}
}
/* Appends the child list to the end of the tail and updates
* the tail.
*/
void append( Node *child, Node **tail ){
Node *curNode;
/* Append the child child list to the end */
(*tail)->next = child;
child->prev = *tail;
/*Find the new tail, which is the end of the child child
*list.
*/
for( curNode = child; curNode->next;
curNode = curNode->next )
; /* Body intentionally empty */
/* Update the tail pointer now that curNode is the new
* tail.
*/
*tail = curNode;
}
我知道我们正在传递**tail
而不是传递*tail
给append
函数以确保更改*tail
持续存在,但我不明白为什么我们不对孩子做同样的事情(意味着我们为什么不传递**child
而不是传递*child
)?