classSolution { public: Node* connect(Node* root){ queue<Node*> qe; if (root) qe.push(root); while (!qe.empty()) { int node_num = qe.size(); for (int i = 0; i < node_num; i++) { auto tmp = qe.front(); qe.pop();
// 设置他的 next 指针 if (!qe.empty() && i != node_num - 1) { auto tmp_next = qe.front(); tmp->next = tmp_next; } if (tmp->left) qe.push(tmp->left); if (tmp->right) qe.push(tmp->right); } }