mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 117.cpp
This commit is contained in:
parent
1be36799dd
commit
8fbe78e621
51
LeetCode/117.cpp
Normal file
51
LeetCode/117.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
|
||||
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;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user