forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
24 lines (23 loc) · 826 Bytes
/
solution.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct TreeLinkNode {
int val;
TreeLinkNode *left, *right, *next;
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
class Solution {
public:
void connect(TreeLinkNode *root) {
while (root and (root->left or root->right)) {
if (root->left and root->right)
root->left->next = root->right;
for (TreeLinkNode *cur = root, *find = root->next; find; find = find->next) {
if (!find->left and !find->right) continue;
if (find->left and find->right)
find->left->next = find->right;
(cur->right ? cur->right->next : cur->left->next) = find->left ? find->left : find->right;
cur = find;
}
root = root->left ? root->left : root->right;
while (root->next && !root->left and !root->right) root = root->next;
}
}
};