aboutsummaryrefslogtreecommitdiff
path: root/leetcode.c
blob: 9ac1b035cd4db1fbd30a79f0f2491d172d196c21 (plain)
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
#include "leetcode.h"

struct ListNode *listnode_create(int val) {
  struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
  new->val = val;
  new->next = NULL;
  return new;
}

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;
}

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);
}