题目:
题解:
struct Node* connect(struct Node* root) {if (root == NULL) {return root;}// 从根节点开始struct Node* leftmost = root;while (leftmost->left != NULL) {// 遍历这一层节点组织成的链表,为下一层的节点更新 next 指针struct Node* head = leftmost;while (head != NULL) {// CONNECTION 1head->left->next = head->right;// CONNECTION 2if (head->next != NULL) {head->right->next = head->next->left;}// 指针向后移动head = head->next;}// 去下一层的最左的节点leftmost = leftmost->left;}return root;
}