-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path109.java
More file actions
73 lines (63 loc) · 2.08 KB
/
109.java
File metadata and controls
73 lines (63 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Runtime: 200 ms, faster than 5.80% of Java online submissions for Convert Sorted List to Binary Search Tree.
// Memory Usage: 44.5 MB, less than 5.26% of Java online submissions for Convert Sorted List to Binary Search Tree.
class Solution {
public TreeNode sortedListToBST(ListNode head) {
if (Objects.isNull(head)) {
return null;
}
int length = getLength(head);
return subtree(head, 0, length - 1);
}
private TreeNode subtree(ListNode head, int start, int end) {
TreeNode root = null;
if (start <= end) {
int mid = (start + end) % 2 == 0 ? (start + end) / 2
: (start + end) / 2 + 1;
root = new TreeNode(getNode(head, mid).val);
root.left = subtree(head, start, mid - 1);
root.right = subtree(head, mid + 1, end);
}
return root;
}
private ListNode getNode(ListNode head, int index) {
for (int i = 0; i < index; i++) {
head = head.next;
}
return head;
}
private int getLength(ListNode head) {
int count = 0;
while (Objects.nonNull(head)) {
head = head.next;
count++;
}
return count;
}
}
// Runtime: 2 ms, faster than 9.48% of Java online submissions for Convert Sorted List to Binary Search Tree.
// Memory Usage: 44.7 MB, less than 5.26% of Java online submissions for Convert Sorted List to Binary Search Tree.
class Solution {
public TreeNode sortedListToBST(ListNode head) {
if (Objects.isNull(head)) {
return null;
}
List<Integer> list = new ArrayList<>();
ListNode cur = head;
while (Objects.nonNull(cur)) {
list.add(cur.val);
cur = cur.next;
}
return subtree(list, 0, list.size() - 1);
}
private TreeNode subtree(List list, int start, int end) {
TreeNode root = null;
if (start <= end) {
int mid = (start + end) % 2 == 0 ? (start + end) / 2
: (start + end) / 2 + 1;
root = new TreeNode((int)list.get(mid));
root.left = subtree(list, start, mid - 1);
root.right = subtree(list, mid + 1, end);
}
return root;
}
}