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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
#include "leetcode.h"
struct two_d_arr *two_d_arr_init(int row_size, int col_size,
int arr[][col_size]) {
struct two_d_arr *obj = (struct two_d_arr *)malloc(sizeof(struct two_d_arr));
obj->row_size = row_size;
obj->col_size = (int *)malloc(row_size * sizeof(int));
obj->arr = (int **)malloc(row_size * sizeof(int *));
for (int i = 0; i < row_size; i++) {
obj->col_size[i] = col_size;
obj->arr[i] = (int *)malloc(col_size * sizeof(int));
memcpy(obj->arr[i], arr[i], col_size * sizeof(int));
}
return obj;
}
void two_d_arr_free(struct two_d_arr *obj) {
for (int i = 0; i < obj->row_size; i++)
free(obj->arr[i]);
free(obj->arr);
free(obj->col_size);
}
struct ListNode *listnode_create(int val) {
struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
new->val = val;
new->next = NULL;
return new;
}
struct ListNode *listnode_build(int *vals, int size) {
if (!size)
return NULL;
struct ListNode *head = listnode_create(vals[0]), *curr = head;
for (int i = 1; i < size; i++) {
curr->next = listnode_create(vals[i]);
curr = curr->next;
}
return head;
}
void listnode_print(struct ListNode *head) {
struct ListNode *curr = head;
while (curr != NULL) {
printf("%d ", curr->val);
curr = curr->next;
}
printf("\n");
}
void listnode_free(struct ListNode *head) {
struct ListNode *tmp;
while (head) {
tmp = head;
head = head->next;
free(tmp);
}
}
struct TreeNode *treenode_create(int val) {
struct TreeNode *new = (struct TreeNode *)malloc(sizeof(struct TreeNode));
new->val = val;
new->left = NULL;
new->right = NULL;
return new;
}
struct TreeNode *treenode_build(int *vals, int size) {
if (!size || vals[0] == -1)
return NULL;
struct TreeNode *root = treenode_create(vals[0]);
struct TreeNode **queue =
(struct TreeNode **)malloc(size * sizeof(struct TreeNode *));
queue[0] = root;
int front = 0, rear = 1;
for (int i = 1; i < size; i++) {
struct TreeNode *curr = queue[front];
if (vals[i] != -1) {
curr->left = treenode_create(vals[i]);
queue[rear++] = curr->left;
}
if (i + 1 < size && vals[i + 1] != -1) {
curr->right = treenode_create(vals[i + 1]);
queue[rear++] = curr->right;
}
i++;
front++;
}
free(queue);
return root;
}
void treenode_print(struct TreeNode *root) {
if (!root)
return;
treenode_print(root->left);
printf("%d ", root->val);
treenode_print(root->right);
}
void treenode_free(struct TreeNode *root) {
if (!root)
return;
treenode_free(root->left);
treenode_free(root->right);
free(root);
}
struct Node *node_create(int val, int numChildren) {
struct Node *new = (struct Node *)malloc(sizeof(struct Node));
new->val = val;
new->numChildren = numChildren;
if (numChildren) {
new->children = (struct Node **)malloc(numChildren * sizeof(struct Node *));
for (int i = 0; i < numChildren; i++)
new->children[i] = NULL;
} else
new->children = NULL;
return new;
}
void node_print(struct Node *root, int depth) {
if (!root)
return;
printf("%d\n", root->val);
for (int i = 0; i < root->numChildren; i++)
node_print(root->children[i], depth + 1);
}
void node_free(struct Node *root) {
if (!root)
return;
for (int i = 0; i < root->numChildren; i++)
node_free(root->children[i]);
free(root->children);
free(root);
}
|