-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path116.cpp
30 lines (30 loc) · 833 Bytes
/
116.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
void connect(TreeLinkNode *root) {
if (!root)
return;
TreeLinkNode *prev = nullptr, *head = root;
while (root) {
while (root) {
if (root->left) {
if (prev)
prev->next = root->left;
else
head = root->left;
prev = root->left;
}
if (root->right) {
if (prev)
prev->next = root->right;
else
head = root->right;
prev = root->right;
}
root = root->next;
}
root = head;
head = nullptr;
prev = nullptr;
}
}
};