forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
116.c
49 lines (38 loc) · 1.15 KB
/
116.c
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
47
48
49
#include <stdio.h>
#include <stdlib.h>
struct TreeLinkNode {
int val;
struct TreeLinkNode *left, *right, *next;
};
void connectHelper(struct TreeLinkNode *node, struct TreeLinkNode *sibling) {
if (node == NULL) return;
if (!node->left || !node->right) return;
/* node have two children */
node->left->next = node->right;
node->right->next = (sibling == NULL) ? NULL : sibling->left;
connectHelper(node->left, node->right);
connectHelper(node->right, node->right->next);
}
void connect(struct TreeLinkNode *root) {
if (root == NULL) return;
root->next = NULL;
connectHelper(root, NULL);
}
int main() {
struct TreeLinkNode *root = (struct TreeLinkNode *)calloc(7, sizeof(struct TreeLinkNode));
root->val = 1;
root->left = root + 1;
root->left->val = 2;
root->right = root + 2;
root->right->val = 3;
root->left->left = root + 3;
root->left->left->val = 4;
root->left->right = root + 4;
root->left->right->val = 5;
root->right->left = root + 5;
root->right->left->val = 6;
root->right->right = root + 6;
root->right->right->val = 7;
connect(root);
return 0;
}