Skip to content

Commit 7975acb

Browse files
committed
feat:add Solution.cpp for 0116. Populating Next Right Pointers in Each Node
1 parent 501e6d6 commit 7975acb

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)