Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit93ad4de

Browse files
refactor 701
1 parent13cb0fd commit93ad4de

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
importcom.fishercoder.common.classes.TreeNode;
44

5+
importjava.util.ArrayList;
6+
importjava.util.List;
7+
58
publicclass_701 {
69
publicstaticclassSolution1 {
710
publicTreeNodeinsertIntoBST(TreeNoderoot,intval) {
@@ -16,4 +19,47 @@ public TreeNode insertIntoBST(TreeNode root, int val) {
1619
returnroot;
1720
}
1821
}
22+
23+
publicstaticclassSolution2 {
24+
publicTreeNodeinsertIntoBST(TreeNoderoot,intval) {
25+
List<Integer>list =newArrayList<>();
26+
inorder(root,list);
27+
intinsertionPoint =binarySearch(list,val);
28+
list.add(insertionPoint,val);
29+
returnformBST(list);
30+
}
31+
32+
privateTreeNodeformBST(List<Integer>list) {
33+
if (list.isEmpty()) {
34+
returnnull;
35+
}
36+
TreeNodenewRoot =newTreeNode(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+
returnnewRoot;
40+
}
41+
42+
privateintbinarySearch(List<Integer>list,inttarget) {
43+
intleft =0;
44+
intright =list.size() -1;
45+
while (left <right) {
46+
intmid =left + (right -left) /2;
47+
if (list.get(mid) <target) {
48+
left =mid +1;
49+
}else {
50+
right =mid -1;
51+
}
52+
}
53+
returnleft ==right ?list.get(left) >=target ?left :left +1 :left;//here's the most tricky/easy to get buggy part!
54+
}
55+
56+
privatevoidinorder(TreeNoderoot,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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
importcom.fishercoder.common.utils.TreeUtils;
5+
importcom.fishercoder.solutions._701;
6+
importorg.junit.BeforeClass;
7+
importorg.junit.Test;
8+
9+
importjava.util.Arrays;
10+
11+
publicclass_701Test {
12+
privatestatic_701.Solution1solution1;
13+
privatestatic_701.Solution2solution2;
14+
15+
@BeforeClass
16+
publicstaticvoidsetup() {
17+
solution1 =new_701.Solution1();
18+
solution2 =new_701.Solution2();
19+
}
20+
21+
@Test
22+
publicvoidtest1() {
23+
intval =88;
24+
TreeNoderoot =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+
publicvoidtest2() {
31+
intval =88;
32+
TreeNoderoot =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+
publicvoidtest3() {
39+
intval =5;
40+
TreeNoderoot =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)

[8]ページ先頭

©2009-2025 Movatter.jp