Skip to content

Commit 05c3cdf

Browse files
refactor 270
1 parent 29e52e6 commit 05c3cdf

File tree

1 file changed

+9
-11
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+9
-11
lines changed

src/main/java/com/fishercoder/solutions/_270.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5-
/**Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
6-
7-
Note:
8-
Given target value is a floating point.
9-
You are guaranteed to have only one unique value in the BST that is closest to the target.*/
105
public class _270 {
116

12-
class GeneralTreeSolution {
13-
//this finished in 1 ms
7+
public static class Solution1 {
8+
//A general tree solution, this finished in 1 ms
149
public int closestValue(TreeNode root, double target) {
1510
if (root == null) {
1611
return 0;
@@ -36,8 +31,9 @@ private int dfs(TreeNode root, double target, double delta, int closestVal) {
3631
}
3732
}
3833

39-
class BSTSolutionRecursive {
40-
//we can tailor the solution to use the BST feature: left subtrees are always smaller than the root the right subtrees
34+
public static class Solution2 {
35+
// BST solution
36+
// we can tailor the solution to use the BST feature: left subtrees are always smaller than the root the right subtrees
4137
//this finished in 0 ms
4238
public int closestValue(TreeNode root, double target) {
4339
if (root == null) {
@@ -62,7 +58,8 @@ private int dfs(TreeNode root, double target, int minVal) {
6258
}
6359
}
6460

65-
class GeneralTreeSolutionMoreConcise {
61+
public static class Solution3 {
62+
//a more concise solution
6663
public int closestValue(TreeNode root, double target) {
6764
if (root == null) {
6865
return 0;
@@ -83,7 +80,8 @@ private int dfs(TreeNode root, double target, int minVal) {
8380
}
8481
}
8582

86-
class BSTSolutionIterative {
83+
public static class Solution4 {
84+
//BST iterative solution
8785
public int closestValue(TreeNode root, double target) {
8886
long minVal = Long.MAX_VALUE;
8987
while (root != null) {

0 commit comments

Comments
 (0)