-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path109.java
42 lines (42 loc) · 1.03 KB
/
109.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
/**
* 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; }
* }
*/
class Solution {
private ListNode node;
public TreeNode sortedListToBST(ListNode head) {
if(head==null) return null;
node = head;
ListNode temp = head;
int size = 0;
while(temp!=null){
size++;
temp = temp.next;
}
return sortedListToBST(0,size-1);
}
private TreeNode sortedListToBST(int start,int end){
if(end<start) return null;
int mid = (start+end)>>>1;
TreeNode left = sortedListToBST(start,mid-1);
TreeNode root = new TreeNode(node.val);
node = node.next;
root.left = left;
TreeNode right = sortedListToBST(mid+1,end);
root.right = right;
return root;
}
}