-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortedListToBST.java
43 lines (38 loc) · 1.05 KB
/
SortedListToBST.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
package brian;
/**
* Created by brian on 10/15/17.
*/
public class SortedListToBST {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
private ListNode currentNode;
public TreeNode sortedListToBST(ListNode head) {
int lengthOfList = 0;
ListNode n = head;
while (n != null) {
n = n.next;
lengthOfList++;
}
currentNode = head;
return sortedListToBST(0, lengthOfList - 1);
}
private TreeNode sortedListToBST(int start, int end) {
if (start > end) return null;
int mid = (start + end) / 2;
TreeNode leftChild = sortedListToBST(start, mid - 1);
TreeNode treeNode = new TreeNode(currentNode.val);
treeNode.left = leftChild;
currentNode = currentNode.next;
treeNode.right = sortedListToBST(mid + 1, end);
return treeNode;
}
}