- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path116.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>
structTreeLinkNode {
intval;
structTreeLinkNode*left, *right, *next;
};
voidconnectHelper(structTreeLinkNode*node, structTreeLinkNode*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);
}
voidconnect(structTreeLinkNode*root) {
if (root==NULL) return;
root->next=NULL;
connectHelper(root, NULL);
}
intmain() {
structTreeLinkNode*root= (structTreeLinkNode*)calloc(7, sizeof(structTreeLinkNode));
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);
return0;
}