> For the complete documentation index, see [llms.txt](https://ucan.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ucan.gitbook.io/notes/tree/109.-convert-sorted-list-to-binary-search-tree.md).

# 109. Convert Sorted List to Binary Search Tree

{% hint style="info" %}
two pointers + inorder traversal
{% endhint %}

```java
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;
    }
}
```
