Skip to content

Commit 9fb0f01

Browse files
add 783
1 parent a8deb5e commit 9fb0f01

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|783|[Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_783.java) | O(n) | O(h) | |Easy|
2526
|779|[K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_779.java) | O(logn) | O(1) | |Medium|
2627
|776|[Split BST](https://leetcode.com/problems/split-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_776.java) | O(n) | O(n) | |Medium| Recursion
2728
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_771.java) | O(n) | O(m) | |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* 783. Minimum Distance Between BST Nodes
9+
*
10+
* Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.
11+
12+
Example :
13+
14+
Input: root = [4,2,6,1,3,null,null]
15+
Output: 1
16+
Explanation:
17+
Note that root is a TreeNode object, not an array.
18+
19+
The given tree [4,2,6,1,3,null,null] is represented by the following diagram:
20+
21+
4
22+
/ \
23+
2 6
24+
/ \
25+
1 3
26+
27+
while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.
28+
29+
Note:
30+
31+
The size of the BST will be between 2 and 100.
32+
The BST is always valid, each node's value is an integer, and each node's value is different.
33+
*/
34+
35+
public class _783 {
36+
public static class Solution1 {
37+
public int minDiffInBST(TreeNode root) {
38+
List<Integer> inorder = new ArrayList<>();
39+
inorder(root, inorder);
40+
return findMinDiff(inorder);
41+
}
42+
43+
private int findMinDiff(List<Integer> inorder) {
44+
int minDiff = Integer.MAX_VALUE;
45+
for (int i = 1; i < inorder.size(); i++) {
46+
minDiff = Math.min(minDiff, inorder.get(i) - inorder.get(i - 1));
47+
}
48+
return minDiff;
49+
}
50+
51+
private void inorder(TreeNode root, List<Integer> inorder) {
52+
if (root == null) {
53+
return;
54+
}
55+
inorder(root.left, inorder);
56+
inorder.add(root.val);
57+
inorder(root.right, inorder);
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._783;
6+
import java.util.Arrays;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _783Test {
13+
private static _783.Solution1 solution1;
14+
private static TreeNode root;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _783.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 6, 1, 3, null, null));
24+
TreeUtils.printBinaryTree(root);
25+
assertEquals(1, solution1.minDiffInBST(root));
26+
}
27+
}

0 commit comments

Comments
 (0)