-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0530-Minimum_Absolute_Difference_in_BST.java
More file actions
127 lines (115 loc) · 3.32 KB
/
0530-Minimum_Absolute_Difference_in_BST.java
File metadata and controls
127 lines (115 loc) · 3.32 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
* 0530-Minimum_Absolute_Difference_in_BST.java
* Billy.Ljm
* 14 June 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/minimum-absolute-difference-in-bst/
*
* 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.
*
* ===========
* My Approach
* ===========
* Since we are given a binary search tree, in-order traversal would yield the
* nodes in ascending order. We can then simply find the difference b/w adjacent
* nodes and remember the minimum absolute difference.
*
* This would have a time complexity of O(n) and space complexity of O(1), where
* n is the number of nodes in the tree
******************************************************************************/
import java.util.Arrays;
import java.util.Queue;
import java.util.LinkedList;
/**
* Definition for a binary tree node.
*/
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
private int prev = Integer.MAX_VALUE; // value of previous node
private int minDiff = Integer.MAX_VALUE; // minimum absolute difference
/**
* Recursively traverse the tree in-order and update the minimum difference
*
* @param node current node
*/
private void recurse(TreeNode node) {
// traverse left first
if (node.left != null) recurse(node.left);
// find difference w/ previous/left node
minDiff = Math.min(minDiff, Math.abs(prev - node.val));
// traverse current node
prev = node.val;
// traverse right
if (node.right!= null) recurse(node.right);
}
/**
* Finds the minimum absolute difference between two nodes in a binary
* search tree
*
* @param root root of the binary search tree
* @return minimum absolute difference between any two nodes
*/
public int getMinimumDifference(TreeNode root) {
recurse(root);
return minDiff;
}
}
class Test {
/**
* Create binary tree from array of integers
*/
private static TreeNode createTree(Integer[] array) {
TreeNode root = new TreeNode(array[0]); // root of tree
// queue of nodes to add children to
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
// iteratively add remaining nodes
TreeNode node = root;
for(int i = 1; i < array.length; i++) {
if (array[i] == null) continue;
else if (i % 2 == 1) {
node = queue.poll();
node.left = new TreeNode(array[i]);
queue.add(node.left);
}
else {
if(array[i] != null) node.right = new TreeNode(array[i]);
queue.add(node.right);
}
}
return root;
}
/**
* Test cases
*/
public static void main(String[] args) {
Solution sol = new Solution();
Integer[] nodes;
TreeNode root;
// test case 1
nodes = new Integer[] {4, 2, 6, 1, 3};
root = createTree(nodes);
System.out.println("getMinimumDifference(" + Arrays.toString(nodes) + ") = "
+ sol.getMinimumDifference(root));
// test case 2
nodes = new Integer[] {1, 0, 48, null, null, 12, 49};
root = createTree(nodes);
System.out.println("getMinimumDifference(" + Arrays.toString(nodes) + ") = "
+ sol.getMinimumDifference(root));
}
}