-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathMinimumAbsoluteDifferenceInBST530.java
60 lines (56 loc) · 1.53 KB
/
MinimumAbsoluteDifferenceInBST530.java
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
/**
* Given a binary search tree with non-negative values, find the minimum
* absolute difference between values of any two nodes.
*
* Example:
*
* Input:
*
* 1
* \
* 3
* /
* 2
*
* Output:
* 1
*
* Explanation:
* The minimum absolute difference is 1, which is the difference between 2
* and 1 (or between 2 and 3).
*
* Note: There are at least two nodes in this BST.
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class MinimumAbsoluteDifferenceInBST530 {
public int getMinimumDifference(TreeNode root) {
int[] res = new int[]{Integer.MAX_VALUE};
helper(root, res);
return res[0];
}
public int[] helper(TreeNode root, int[] res) {
int[] bound = new int[]{root.val, root.val};
if (root.left == null && root.right == null) return bound;
if (root.left != null) {
int[] leftRes = helper(root.left, res);
bound[0] = Math.min(bound[0], leftRes[0]);
bound[1] = Math.max(bound[1], leftRes[1]);
res[0] = Math.min(res[0], Math.abs(leftRes[1] - root.val));
}
if (root.right != null) {
int[] rightRes = helper(root.right, res);
bound[0] = Math.min(bound[0], rightRes[0]);
bound[1] = Math.max(bound[1], rightRes[1]);
res[0] = Math.min(res[0], Math.abs(rightRes[0] - root.val));
}
return bound;
}
}