leetcoders/LeetCode — pull request #20 by aonaiyong (2014-09-25 13:17:00+00:00)
Title: succinct and maybe more efficient solution 

void connectQueue(TreeLinkNode *root) {
if (!root) return;

queue q;
q.push(root);
while (!q.empty()) {
TreeLinkNode *prev = nullptr, *curr = nullptr;
int n = q.size();
for (int i = 0; i < n; ++i) {
curr = q.front(); q.pop();
if (prev) prev->next = curr;
prev = curr;

if (curr->left) q.push(curr->left);
if (curr->right) q.push(curr->right);
}
}
}