-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode116.cpp
More file actions
46 lines (36 loc) · 893 Bytes
/
leetcode116.cpp
File metadata and controls
46 lines (36 loc) · 893 Bytes
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
Node* connect(Node* root) {
if (root == NULL) {
return root;
}
queue<Node*> q;
q.push(root);
q.push(NULL);
Node* prev = NULL;
while (!q.empty()) {
Node* curr = q.front();
q.pop();
if (curr == NULL) {
prev = NULL;
if (q.empty()) {
break;
}
q.push(NULL);
}
else {
if (prev != NULL) {
prev->next = curr;
}
prev = curr;
if (curr->left != NULL) {
q.push(curr->left);
}
if (curr->right != NULL) {
q.push(curr->right);
}
}
}
return root;
}
};