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

Commit146c301

Browse files
refactor 109
1 parent63c6c59 commit146c301

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

‎src/main/java/com/fishercoder/solutions/_109.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,34 @@
44
importcom.fishercoder.common.classes.TreeNode;
55

66
/**
7+
* 109. Convert Sorted List to Binary Search Tree
8+
*
79
* Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
810
*/
911
publicclass_109 {
1012

11-
publicTreeNodesortedListToBST(ListNodehead) {
12-
returnrec(head,null);
13-
}
13+
publicstaticclassSolution1 {
1414

15-
publicTreeNoderec(ListNodestart,ListNodeend) {
16-
if (start ==end) {
17-
returnnull;
18-
}else {
19-
ListNodemid =start;
20-
ListNodeprobe =start;
21-
while (probe !=end &&probe.next !=end) {
22-
mid =mid.next;
23-
probe =probe.next.next;
24-
}
15+
publicTreeNodesortedListToBST(ListNodehead) {
16+
returntoBstRecursively(head,null);
17+
}
2518

26-
TreeNoderoot =newTreeNode(mid.val);
27-
root.left =rec(start,mid);
28-
root.right =rec(mid.next,end);
29-
returnroot;
19+
publicTreeNodetoBstRecursively(ListNodestart,ListNodeend) {
20+
if (start ==end) {
21+
returnnull;
22+
}else {
23+
ListNodemid =start;
24+
ListNodefast =start;
25+
while (fast !=end &&fast.next !=end) {
26+
mid =mid.next;
27+
fast =fast.next.next;
28+
}
29+
30+
TreeNoderoot =newTreeNode(mid.val);
31+
root.left =toBstRecursively(start,mid);
32+
root.right =toBstRecursively(mid.next,end);
33+
returnroot;
34+
}
3035
}
3136
}
3237

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.classes.TreeNode;
5+
importcom.fishercoder.common.utils.LinkedListUtils;
6+
importcom.fishercoder.common.utils.TreeUtils;
7+
importcom.fishercoder.solutions._109;
8+
importorg.junit.Before;
9+
importorg.junit.BeforeClass;
10+
importorg.junit.Test;
11+
12+
importjava.util.Arrays;
13+
14+
publicclass_109Test {
15+
privatestatic_109.Solution1solution1;
16+
privatestaticListNodehead;
17+
privatestaticTreeNodeexpected;
18+
19+
@BeforeClass
20+
publicstaticvoidsetup() {
21+
solution1 =new_109.Solution1();
22+
}
23+
24+
@Before
25+
publicvoidsetUp()throwsException {
26+
}
27+
28+
@Test
29+
publicvoidtest1() {
30+
head =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4,5});
31+
expected =TreeUtils.constructBinaryTree(Arrays.asList(3,1,4,null,2,null,5));
32+
/**as long as it's a height-balanced tree, it's good for this problem requirement*/
33+
TreeUtils.printBinaryTree(solution1.sortedListToBST(head));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp