-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimumAbsoluteDifferenceInBST.java
More file actions
53 lines (37 loc) · 1.57 KB
/
Copy pathMinimumAbsoluteDifferenceInBST.java
File metadata and controls
53 lines (37 loc) · 1.57 KB
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
// Given a binary search tree with non-negative values,
// find the minimum absolute difference between values of any two nodes.
// See: https://leetcode.com/problems/minimum-absolute-difference-in-bst/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.*;
import java.util.ArrayList;
import java.util.List;
import leetcode.util.tree.TreeNode;
public class MinimumAbsoluteDifferenceInBST {
private int minDiff = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) {
helper(root, new ArrayList<>());
return minDiff;
}
private void helper(TreeNode root, List<Integer> inorderList) {
if (root != null) {
helper(root.left, inorderList);
if (!inorderList.isEmpty()) {
int currDiff = Math.abs(root.val - inorderList.get(inorderList.size() - 1));
minDiff = Math.min(currDiff, minDiff);
}
inorderList.add(root.val);
helper(root.right, inorderList);
}
}
public static void main(String[] args) {
TreeNode t1 = initTree(1, null, 3, 2, null);
System.out.println(new MinimumAbsoluteDifferenceInBST()
.getMinimumDifference(t1));
System.out.println(new MinimumAbsoluteDifferenceInBST()
.getMinimumDifference(initTree(1)));
System.out.println(new MinimumAbsoluteDifferenceInBST()
.getMinimumDifference(initTree()));
System.out.println(new MinimumAbsoluteDifferenceInBST()
.getMinimumDifference(initTree(5, 4, 7)));
}
}