aboutsummaryrefslogtreecommitdiff
path: root/min_abs_diff_bst.c
blob: ad3db74f906de07f10f333406f09443b3470e297 (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
// 530. Minimum Absolute Difference in BST
#include <limits.h>
#include <stdlib.h>

/*
 * given the 'root' of a binary search tree (bst), return the minimum absolute
 * difference between the values of any two different nodes in the tree
 */

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

inline int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}

void process(struct TreeNode *root, int *val) {
  struct TreeNode *tmp;
  if (root->left) {
    tmp = root->left;
    while (tmp->right)
      tmp = tmp->right;
    *val = min(*val, root->val - tmp->val);
    if (*val == 1)
      return;
    process(root->left, val);
  }
  if (root->right) {
    tmp = root->right;
    while (tmp->left)
      tmp = tmp->left;
    *val = min(*val, tmp->val - root->val);
    if (*val == 1)
      return;
    process(root->right, val);
  }
}

int getMinimumDifference(struct TreeNode *root) {
  int *val = malloc(sizeof(int));
  *val = INT_MAX;
  process(root, val);
  int ans = *val;
  free(val);
  return ans;
}