We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 501e6d6 commit 7975acbCopy full SHA for 7975acb
solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.cpp
@@ -0,0 +1,21 @@
1
+class Solution {
2
+public:
3
+ Node* connect(Node* root) {
4
+ if (!root) return nullptr;
5
+ queue<Node*> q;
6
+ q.push(root);
7
+ while (!q.empty()) {
8
+ int size = q.size();
9
+ for (int i = 0; i < size; ++i) {
10
+ Node* t = q.front();
11
+ q.pop();
12
+ if (i < size - 1) {
13
+ t->next = q.front();
14
+ }
15
+ if (t->left) q.push(t->left);
16
+ if (t->right) q.push(t->right);
17
18
19
+ return root;
20
21
+};
0 commit comments