Skip to content

Commit 233aa4c

Browse files
[LEET-530] add 530
1 parent 9dd84cd commit 233aa4c

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Algorithms
44
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
55
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
6+
|530|[Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinimumAbsoluteDifferenceinBST.java) | O(n) |O(n) | Easy| DFS
67
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DetectCapital.java) | O(n) |O(1) | Easy|
78
|515|[Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindLargestValueinEachTreeRow.java) | O(n) |O(k) | Medium| BFS
89
|513|[Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindBottomLeftValue.java) | O(n) |O(k) | Medium| BFS
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.stevesun.solutions;
2+
3+
import com.stevesun.common.classes.TreeNode;
4+
5+
import java.util.Iterator;
6+
import java.util.TreeSet;
7+
8+
/**
9+
* Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
10+
11+
Example:
12+
13+
Input:
14+
15+
1
16+
\
17+
3
18+
/
19+
2
20+
21+
Output:
22+
1
23+
24+
Explanation:
25+
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
26+
Note: There are at least two nodes in this BST.
27+
*/
28+
public class MinimumAbsoluteDifferenceinBST {
29+
30+
public int getMinimumDifference(TreeNode root) {
31+
TreeSet<Integer> treeset = new TreeSet<>();
32+
treeset.add(root.val);
33+
dfs(root, treeset);
34+
int diff = Integer.MAX_VALUE;
35+
Iterator<Integer> iterator = treeset.iterator();
36+
int prev = iterator.next();
37+
while (iterator.hasNext()) {
38+
int current = iterator.next();
39+
diff = Math.min(diff, Math.abs(current - prev));
40+
prev = current;
41+
}
42+
return diff;
43+
}
44+
45+
private void dfs(TreeNode root, TreeSet<Integer> treeset) {
46+
if (root.left != null) {
47+
treeset.add(root.left.val);
48+
dfs(root.left, treeset);
49+
}
50+
if (root.right != null) {
51+
treeset.add(root.right.val);
52+
dfs(root.right, treeset);
53+
}
54+
}
55+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.common.classes.TreeNode;
4+
import com.stevesun.solutions.MinimumAbsoluteDifferenceinBST;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static junit.framework.Assert.assertEquals;
10+
11+
public class MinimumAbsoluteDifferenceinBSTTest {
12+
private static MinimumAbsoluteDifferenceinBST test;
13+
private static int expected;
14+
private static int actual;
15+
private static TreeNode root;
16+
17+
@BeforeClass
18+
public static void setup(){
19+
test = new MinimumAbsoluteDifferenceinBST();
20+
}
21+
22+
@Before
23+
public void setupForEachTest(){
24+
expected = 0;
25+
actual = 0;
26+
}
27+
28+
@Test
29+
public void test1(){
30+
root = new TreeNode(1);
31+
root.right = new TreeNode(3);
32+
root.right.left = new TreeNode(2);
33+
expected = 1;
34+
actual = test.getMinimumDifference(root);
35+
assertEquals(expected, actual);
36+
}
37+
38+
@Test
39+
public void test2(){
40+
root = new TreeNode(1);
41+
root.right = new TreeNode(5);
42+
root.right.left = new TreeNode(3);
43+
expected = 2;
44+
actual = test.getMinimumDifference(root);
45+
assertEquals(expected, actual);
46+
}
47+
48+
// [543,384,652,null,445,null,699]
49+
@Test
50+
public void test3() {
51+
root = new TreeNode(543);
52+
root.left = new TreeNode(384);
53+
root.right = new TreeNode(652);
54+
root.left.right = new TreeNode(445);
55+
root.right.right = new TreeNode(699);
56+
expected = 47;
57+
actual = test.getMinimumDifference(root);
58+
assertEquals(expected, actual);
59+
}
60+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy