Skip to content

Commit 412dfc4

Browse files
committed
Merge branch 'main' of github.com:doocs/leetcode into main
2 parents 08fa6f6 + 524a8ba commit 412dfc4

File tree

2 files changed

+43
-0
lines changed
  • solution/0100-0199

2 files changed

+43
-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+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
20+
return root;
21+
}
22+
};

0 commit comments

Comments
 (0)