aboutsummaryrefslogtreecommitdiff
path: root/min_dist_bst_node.c
blob: 8a5d8865b33b84d6149eedbd65c7892041e8c56d (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
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>

/* 783. Minimum Distance Between BST Nodes
 * given the 'root' of BST, return the minimum difference between the values
 * of any two different nodes in the tree!
 */

struct TreeNode {
  int val;
  struct TreeNode *left;
  struct TreeNode *right;
};

void tree2array(struct TreeNode *root, int *data, int *idx) {
  if (root == NULL)
    return;
  tree2array(root->left, data, idx);
  data[*idx] = root->val;
  *idx = *idx + 1;
  tree2array(root->right, data, idx);
}

int minDiffInBST(struct TreeNode *root) {
  int *data = malloc(100 * sizeof(int));
  int *idx = calloc(1, sizeof(int));
  tree2array(root, data, idx);
  int min = INT_MAX;
  for (int i = 1; i < (*idx); i++)
    if ((data[i] - data[i - 1]) < min)
      min = data[i] - data[i - 1];
  free(data);
  free(idx);
  return min;
}