8000 refactor 109 · codingwhite/Leetcode-4@146c301 · GitHub
[go: up one dir, main page]

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

Appearance settings

Commit 146c301

Browse files
refactor 109
1 parent 63c6c59 commit 146c301

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
import com.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
public class _109 {
1012

11-
public TreeNode sortedListToBST(ListNode head) {
12-
return rec(head, null);
13-
}
13+
public static class Solution1 {
1414

15-
public TreeNode rec(ListNode start, ListNode end) {
16-
if (start == end) {
17-
return null;
18-
} else {
19-
ListNode mid = start;
20-
ListNode probe = start;
21-
while (probe != end && probe.next != end) {
22-
mid = mid.next;
23-
probe = probe.next.next;
24-
}
15+
public TreeNode sortedListToBST(ListNode head) {
16+
return toBstRecursively(head, null);
17+
}
2518

26-
TreeNode root = new TreeNode(mid.val);
27-
root.left = rec(start, mid);
28-
root.right = rec(mid.next, end);
29-
return root;
19+
public TreeNode toBstRecursively(ListNode start, ListNode end) {
20+
if (start == end) {
21+
return null;
22+
} else {
23+
ListNode mid = start;
24+
ListNode fast = start;
25+
while (fast != end && fast.next != end) {
26+
mid = mid.next;
27+
fast = fast.next.next;
28+
}
29+
30+
TreeNode root = new TreeNode(mid.val);
31+
root.left = toBstRecursively(start, mid);
32+
root.right = toBstRecursively(mid.next, end);
33+
return root;
34+
}
3035
}
3136
}
3237

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
import com.fishercoder.common.classes.TreeNode;
5+
import com.fishercoder.common.utils.LinkedListUtils;
6+
import com.fishercoder.common.utils.TreeUtils;
7+
import com.fishercoder.solutions._109;
8+
import org.junit.Before;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
12+
import java.util.Arrays;
13+
14+
public class _109Test {
15+
private static _109.Solution1 solution1;
16+
private static ListNode head;
17+
private static TreeNode expected;
18+
19+
@BeforeClass
20+
public static void setup() {
21+
solution1 = new _109.Solution1();
22+
}
23+
24+
@Before
25+
public void setUp() throws Exception {
26+
}
27+
28+
@Test
29+
public void test1() {
30+
head = LinkedListUtils.contructLinkedList(new int[]{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)
0