diff --git a/LeetCode/117.cpp b/LeetCode/117.cpp new file mode 100644 index 0000000..090ac1b --- /dev/null +++ b/LeetCode/117.cpp @@ -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; + } +};