Skip to content

Commit 93ad4de

Browse files
refactor 701
1 parent 13cb0fd commit 93ad4de

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

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

5+
import java.util.ArrayList;
6+
import java.util.List;
7+
58
public class _701 {
69
public static class Solution1 {
710
public TreeNode insertIntoBST(TreeNode root, int val) {
@@ -16,4 +19,47 @@ public TreeNode insertIntoBST(TreeNode root, int val) {
1619
return root;
1720
}
1821
}
22+
23+
public static class Solution2 {
24+
public TreeNode insertIntoBST(TreeNode root, int val) {
25+
List<Integer> list = new ArrayList<>();
26+
inorder(root, list);
27+
int insertionPoint = binarySearch(list, val);
28+
list.add(insertionPoint, val);
29+
return formBST(list);
30+
}
31+
32+
private TreeNode formBST(List<Integer> list) {
33+
if (list.isEmpty()) {
34+
return null;
35+
}
36+
TreeNode newRoot = new TreeNode(list.get(list.size() / 2));
37+
newRoot.left = formBST(list.subList(0, list.size() / 2));
38+
newRoot.right = formBST(list.subList(list.size() / 2 + 1, list.size()));
39+
return newRoot;
40+
}
41+
42+
private int binarySearch(List<Integer> list, int target) {
43+
int left = 0;
44+
int right = list.size() - 1;
45+
while (left < right) {
46+
int mid = left + (right - left) / 2;
47+
if (list.get(mid) < target) {
48+
left = mid + 1;
49+
} else {
50+
right = mid - 1;
51+
}
52+
}
53+
return left == right ? list.get(left) >= target ? left : left + 1 : left;//here's the most tricky/easy to get buggy part!
54+
}
55+
56+
private void inorder(TreeNode root, List<Integer> list) {
57+
if (root == null) {
58+
return;
59+
}
60+
inorder(root.left, list);
61+
list.add(root.val);
62+
inorder(root.right, list);
63+
}
64+
}
1965
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._701;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
public class _701Test {
12+
private static _701.Solution1 solution1;
13+
private static _701.Solution2 solution2;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _701.Solution1();
18+
solution2 = new _701.Solution2();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
int val = 88;
24+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(61, 46, 66, 43, null, null, null, 39, null, null, null));
25+
TreeUtils.printBinaryTree(root);
26+
TreeUtils.printBinaryTree(solution1.insertIntoBST(root, val));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
int val = 88;
32+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(61, 46, 66, 43, null, null, null, 39, null, null, null));
33+
TreeUtils.printBinaryTree(root);
34+
TreeUtils.printBinaryTree(solution2.insertIntoBST(root, val));
35+
}
36+
37+
@Test
38+
public void test3() {
39+
int val = 5;
40+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(4, 2, 7, 1, 3));
41+
TreeUtils.printBinaryTree(root);
42+
TreeUtils.printBinaryTree(solution2.insertIntoBST(root, val));
43+
}
44+
45+
}

0 commit comments

Comments
 (0)