-
Notifications
You must be signed in to change notification settings - Fork 0
/
Populate.cpp
32 lines (32 loc) · 913 Bytes
/
Populate.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
31
32
class Populate {
public:
Node* connect(Node* root) {
if (root == NULL)
return root;
Node* head = root;
while (head) {
Node* cur = head;
Node* prev = NULL;
Node* next_head = NULL;
while (cur) {
if (cur->left) {
if (prev)
prev->next = cur->left;
prev = cur->left;
if (next_head == NULL)
next_head = cur->left;
}
if (cur->right) {
if (prev)
prev->next = cur->right;
prev = cur->right;
if (next_head == NULL)
next_head = cur->right;
}
cur = cur->next;
}
head = next_head;
}
return root;
}
};