-
Notifications
You must be signed in to change notification settings - Fork 0
/
109_ConvertSortedListToBST109.java
175 lines (138 loc) · 4.45 KB
/
109_ConvertSortedListToBST109.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
*/
import java.util.ArrayList;
import java.util.List;
/*
The following classes are defined in seperate files.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class ConvertSortedListToBST109 {
public TreeNode sortedListToBST(ListNode head) {
List<TreeNode> nodes = new ArrayList<TreeNode>();
storeBSTNodes(head, nodes);
int n = nodes.size();
return buildBST(nodes, 0, n-1);
}
private void storeBSTNodes(ListNode head, List<TreeNode> nodes) {
while (head != null) {
nodes.add(new TreeNode(head.val));
head = head.next;
}
}
private TreeNode buildBST(List<TreeNode> nodes, int start, int end) {
if (start > end)
return null;
int mid = (start + end) / 2;
TreeNode node = nodes.get(mid);
node.left = buildBST(nodes, start, mid - 1);
node.right = buildBST(nodes, mid + 1, end);
return node;
}
/** -------------------------------------------------------------------
* Top Solution: O(n logn)
* https://discuss.leetcode.com/topic/35997/share-my-java-solution-1ms-very-short-and-concise
* --------------------------------------------------------------------
*/
public TreeNode sortedListToBST2(ListNode head) {
if (head == null) return null;
return toBST(head, null);
}
public TreeNode toBST(ListNode head, ListNode tail){
ListNode slow = head;
ListNode fast = head;
if(head == tail) return null;
while(fast != tail && fast.next != tail){
fast = fast.next.next;
slow = slow.next;
}
TreeNode thead = new TreeNode(slow.val);
thead.left = toBST(head, slow);
thead.right = toBST(slow.next, tail);
return thead;
}
/** -------------------------------------------------------------------
* Top Solution: O(n)
* https://discuss.leetcode.com/topic/35997/share-my-java-solution-1ms-very-short-and-concise
* --------------------------------------------------------------------
*/
private ListNode node;
public TreeNode sortedListToBST3(ListNode head) {
if(head == null){
return null;
}
int size = 0;
ListNode runner = head;
node = head;
while(runner != null){
runner = runner.next;
size ++;
}
return inorderHelper(0, size - 1);
}
public TreeNode inorderHelper(int start, int end){
if(start > end){
return null;
}
int mid = start + (end - start) / 2;
TreeNode left = inorderHelper(start, mid - 1);
TreeNode treenode = new TreeNode(node.val);
treenode.left = left;
node = node.next;
TreeNode right = inorderHelper(mid + 1, end);
treenode.right = right;
return treenode;
}
public TreeNode sortedListToBST4(ListNode head) {
if (head == null) return null;
if (head.next == null) return new TreeNode(head.val);
ListNode fast = head;
ListNode slow = head;
ListNode beforeSlow = null;
while (slow != null && fast != null && fast.next != null) {
fast = fast.next.next;
beforeSlow = slow;
slow = slow.next;
}
beforeSlow.next = null;
TreeNode res = new TreeNode(slow.val);
res.left = sortedListToBST(head);
res.right = sortedListToBST(slow.next);
return res;
}
public TreeNode sortedListToBST5(ListNode head) {
if (head == null) return null;
ListNode fast = head;
ListNode slow = head;
ListNode pre = null;
while (fast != null && fast.next != null) {
fast = fast.next.next;
pre = slow;
slow = slow.next;
}
if (pre == null) {
return new TreeNode(head.val);
}
pre.next = null;
TreeNode root = new TreeNode(slow.val);
root.left = sortedListToBST(head);
root.right = sortedListToBST(slow.next);
return root;
}
}