mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
52 lines
1.6 KiB
C++
52 lines
1.6 KiB
C++
|
|
class Solution {
|
|
public:
|
|
Node* processLevel(Node* leftMost) {
|
|
Node* nextLevelLeftMost = nullptr;
|
|
Node* nextLevelNext = nullptr;
|
|
for (Node* current = leftMost; current != nullptr; current = current->next) {
|
|
if (current->left && current->right) {
|
|
current->left->next = current->right;
|
|
if (!nextLevelLeftMost) {
|
|
nextLevelLeftMost = current->left;
|
|
nextLevelNext = current->right;
|
|
}
|
|
else {
|
|
nextLevelNext->next = current->left;
|
|
nextLevelNext = current->right;
|
|
}
|
|
}
|
|
else if (current->left) {
|
|
if (!nextLevelLeftMost) {
|
|
nextLevelLeftMost = current->left;
|
|
nextLevelNext = current->left;
|
|
}
|
|
else {
|
|
nextLevelNext->next = current->left;
|
|
nextLevelNext = current->left;
|
|
}
|
|
}
|
|
else if (current->right) {
|
|
if (!nextLevelLeftMost) {
|
|
nextLevelLeftMost = current->right;
|
|
nextLevelNext = current->right;
|
|
}
|
|
else {
|
|
nextLevelNext->next = current->right;
|
|
nextLevelNext = current->right;
|
|
}
|
|
}
|
|
}
|
|
|
|
return nextLevelLeftMost;
|
|
}
|
|
|
|
Node* connect(Node* root) {
|
|
Node* nextLevelEntry = root;
|
|
while (nextLevelEntry) {
|
|
nextLevelEntry = processLevel(nextLevelEntry);
|
|
}
|
|
return root;
|
|
}
|
|
};
|