class Solution {
public TreeNode sortedListToBST(ListNode head) {
return recursion(head, null);
}
// tail to mark the position that should not be reached
// effective list: from head (include) to tail (not include)
private TreeNode recursion(ListNode head, ListNode tail){
if (head == tail) return null;
ListNode slow = head;
ListNode fast = head;
while(fast!=tail && fast.next!=tail){
slow = slow.next;
fast = fast.next.next;
}
TreeNode node = new TreeNode(slow.val);
node.left= recursion(head, slow);
node.right = recursion(slow.next, tail);
return node;
}
}