-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumAbsoluteDifferenceInBST.java
More file actions
230 lines (168 loc) Β· 7.01 KB
/
MinimumAbsoluteDifferenceInBST.java
File metadata and controls
230 lines (168 loc) Β· 7.01 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package Algorithms.BinaryTrees;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 15 Dec 2025
* @link 530. Minimum Absolute Difference in BST <a href="https://leetcode.com/problems/minimum-absolute-difference-in-bst/">LeetCode Link 1</a>
* @link 783. Minimum Distance Between BST Nodes <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/">LeetCode Link 2</a>
* @topics Tree, Binary Tree, Binary Search Tree, DFS, BFS
* @companies Google(4), Meta(2), Amazon(2), Wix(2)
* LeetCode 530 & 783 problems are same
*/
public class MinimumAbsoluteDifferenceInBST {
public static class TreeNode {int val;TreeNode left, right;TreeNode() {}TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}
public static void main(String[] args) {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(6);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
System.out.println("getMinimumDifference Using Recursion -> " + getMinimumDifferenceUsingRecursion(root));
System.out.println("getMinimumDifference Using Dfs -> " + getMinimumDifferenceUsingDfs(root));
System.out.println("getMinimumDifference Using SortedList π₯ -> " + getMinimumDifferenceUsingSortedList(root));
System.out.println("getMinimumDifference Using InOrderList π₯ -> " + getMinimumDifferenceUsingInOrderList(root));
System.out.println("getMinimumDifference Using InOrderDfs π₯ -> " + getMinimumDifferenceUsingInOrderDfs(root));
System.out.println("getMinimumDifference Using InOrderRecursion -> " + getMinimumDifferenceUsingInOrderRecursion(root));
System.out.println("getMinimumDifference Using DfsWithRange -> " + getMinimumDifferenceUsingDfsWithRange(root));
}
/**
* @TimeComplexity O(nh) β O(n log n), cause in balanced BST h=logn and in unbalanced it's O(n^2)
* @SpaceComplexity O(h) β O(log n) for balanced and O(n) for unbalanced
*/
public static int getMinimumDifferenceUsingRecursion(TreeNode root) {
if (root == null) return Integer.MAX_VALUE;
int min = Integer.MAX_VALUE;
// leftsRightMost -> near smaller number in BST
if (root.left != null) {
TreeNode prev = root.left;
while (prev.right != null) prev = prev.right;
min = Math.min(min, root.val - prev.val);
}
// rightsLeftMost -> near bigger number in BST
if (root.right != null) {
TreeNode next = root.right;
while (next.left != null) next = next.left;
min = Math.min(min, next.val - root.val);
}
min = Math.min(min, getMinimumDifferenceUsingRecursion(root.left));
min = Math.min(min, getMinimumDifferenceUsingRecursion(root.right));
return min;
}
/**
* @TimeComplexity O(nh) β O(n log n), cause in balanced BST h=logn and in unbalanced it's O(n^2)
* @SpaceComplexity O(h) β O(log n) for balanced and O(n) for unbalanced
*/
static int min;
public static int getMinimumDifferenceUsingDfs(TreeNode root) {
min = Integer.MAX_VALUE;
dfs1(root);
return min;
}
private static void dfs1(TreeNode node){
if (node == null) return;
if (node.left != null) {
TreeNode prev = node.left; // leftsRightMost
while (prev.right != null) prev = prev.right;
min = Math.min(min, node.val - prev.val);
}
if (node.right != null) {
TreeNode next = node.right; // rightsLeftMost
while (next.left != null) next = next.left;
min = Math.min(min, next.val - node.val);
}
dfs1(node.left);
dfs1(node.right);
}
/**
* @TimeComplexity O(n log n)
* @SpaceComplexity O(n)
*/
static List<Integer> nodeValues;
public static int getMinimumDifferenceUsingSortedList(TreeNode root) {
nodeValues = new ArrayList<>();
dfs(root); // we can use dfs or bfs for traversal
Collections.sort(nodeValues);
int min = Integer.MAX_VALUE;
for (int i = 1; i < nodeValues.size(); i++) {
min = Math.min(min, nodeValues.get(i) - nodeValues.get(i - 1));
}
return min;
}
private static void dfs(TreeNode node) {
if (node == null) return;
nodeValues.add(node.val);
dfs(node.left);
dfs(node.right);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*/
static List<Integer> inorderNodes;
public static int getMinimumDifferenceUsingInOrderList(TreeNode root) {
inorderNodes = new ArrayList<>();
inorderDfsWithList(root); // In BST, in-order traversal gives the sorted order π₯
int min = Integer.MAX_VALUE;
for (int i = 1; i < inorderNodes.size(); i++) {
min = Math.min(min, inorderNodes.get(i) - inorderNodes.get(i-1));
}
return min;
}
private static void inorderDfsWithList(TreeNode node) {
if (node == null) return;
inorderDfsWithList(node.left);
inorderNodes.add(node.val);
inorderDfsWithList(node.right);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(h)
*/
static int minVal = Integer.MAX_VALUE;
static Integer prev = null;
public static int getMinimumDifferenceUsingInOrderDfs(TreeNode root) {
inOrderDfs(root); // In BST, in-order traversal gives the sorted order π₯
return minVal;
}
private static void inOrderDfs(TreeNode root){
if (root == null) return;
inOrderDfs(root.left);
if (prev != null) {
minVal = Math.min(minVal, Math.abs(root.val - prev));
}
prev = root.val;
inOrderDfs(root.right);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(h)
*/
// Integer prev = null;
public static int getMinimumDifferenceUsingInOrderRecursion(TreeNode root) {
if (root == null) return minVal;
getMinimumDifferenceUsingInOrderRecursion(root.left);
if (prev != null) {
minVal = Math.min(minVal, root.val - prev);
}
prev = root.val;
getMinimumDifferenceUsingInOrderRecursion(root.right);
return minVal;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(h)
*/
public static int getMinimumDifferenceUsingDfsWithRange(TreeNode root) {
return dfs(root, 1000001, -100001);
}
public static int dfs(TreeNode root, int min, int max) {
if (root == null) return Integer.MAX_VALUE;
int minDiff = Math.abs(root.val-min);
int maxDiff = Math.abs(max-root.val);
int diff = Math.min(minDiff, maxDiff);
int subMin = Math.min(dfs(root.left, min, root.val), dfs(root.right, root.val, max));
return Math.min(diff, subMin);
}
}