@@ -0,0 +1,1743 @@
+import org.junit.Test;%0A%0Aimport static org.junit.Assert.assertEquals;%0A%0A/**%0A * https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/%0A */%0Apublic class Solution %7B%0A    public static class ListNode %7B%0A        int val;%0A        ListNode next;%0A%0A        ListNode(int x) %7B%0A            val = x;%0A        %7D%0A    %7D%0A%0A    public static class TreeNode %7B%0A        int val;%0A        TreeNode left;%0A        TreeNode right;%0A%0A        TreeNode(int x) %7B%0A            val = x;%0A        %7D%0A    %7D%0A%0A    ListNode list;%0A%0A    public TreeNode sortedListToBST(ListNode head) %7B%0A        int length = 0;%0A        for (ListNode p = head; p != null; p = p.next, length++) ;%0A        list = head;%0A        return sortedListToBST(0, length - 1);%0A    %7D%0A%0A    private TreeNode sortedListToBST(int start, int end) %7B%0A        if (start %3E end) %7B%0A            return null;%0A        %7D%0A        int mid = (start + end) / 2;%0A        TreeNode left = sortedListToBST(start, mid - 1);%0A        TreeNode root = new TreeNode(list.val);%0A        root.left = left;%0A        list = list.next;%0A        root.right = sortedListToBST(mid + 1, end);%0A        return root;%0A    %7D%0A%0A    @Test%0A    public void test() %7B%0A        ListNode head = new ListNode(1);%0A        ListNode p = head;%0A        p.next = new ListNode(3);%0A        p = p.next;%0A        p.next = new ListNode(5);%0A        p = p.next;%0A        p.next = new ListNode(8);%0A        p = p.next;%0A        p.next = new ListNode(13);%0A        p = p.next;%0A        p.next = new ListNode(16);%0A%0A        Solution solution = new Solution();%0A        TreeNode root = solution.sortedListToBST(head);%0A        assertEquals(5, root.val);%0A        assertEquals(1, root.left.val);%0A        assertEquals(3, root.left.right.val);%0A        assertEquals(13, root.right.val);%0A    %7D%0A%7D%0A

convert-sorted-list-to-binary-search-tree