diff --git a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.cpp b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.cpp new file mode 100644 index 0000000000000..97bed5f8756cd --- /dev/null +++ b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + Node* connect(Node* root) { + if (!root) return nullptr; + queue q; + q.push(root); + while (!q.empty()) { + int size = q.size(); + for (int i = 0; i < size; ++i) { + Node* t = q.front(); + q.pop(); + if (i < size - 1) { + t->next = q.front(); + } + if (t->left) q.push(t->left); + if (t->right) q.push(t->right); + } + } + return root; + } +}; \ No newline at end of file