@@ -0,0 +1,1443 @@
+package tree;%0A%0A/**%0A * https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/%0A * %3Cp/%3E%0A * Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.%0A */%0Apublic class ConvertSortedListBinarySearchTree %7B%0A    private static class ListNode %7B%0A        int val;%0A        ListNode next;%0A%0A        ListNode(int x) %7B%0A            val = x;%0A            next = null;%0A        %7D%0A    %7D%0A%0A    private static class TreeNode %7B%0A        TreeNode(int val) %7B%0A            this.val = val;%0A        %7D%0A%0A        int val;%0A        TreeNode left;%0A        TreeNode right;%0A    %7D%0A%0A    public TreeNode sortedListToBST(ListNode head) %7B%0A        if (head == null) return null;%0A        int count = 0;%0A        ListNode node = head;%0A        while (node != null) %7B%0A            count++;%0A            node = node.next;%0A        %7D%0A        ListNode%5B%5D nodes = new ListNode%5B1%5D;%0A        nodes%5B0%5D = head;%0A        return constructBinarySearchTree(nodes, 0, count - 1);%0A    %7D%0A%0A    private TreeNode constructBinarySearchTree(ListNode%5B%5D nodes, int start, int end) %7B%0A        if (start %3E end) return null;%0A%0A        int mid = start + (end - start) / 2;%0A        TreeNode left = constructBinarySearchTree(nodes, start, mid - 1);%0A        TreeNode root = new TreeNode(nodes%5B0%5D.val);%0A        root.left = left;%0A        nodes%5B0%5D = nodes%5B0%5D.next;%0A        root.right = constructBinarySearchTree(nodes, mid + 1, end);%0A        return root;%0A    %7D%0A%7D%0A

Convert Sorted List to Binary Search Tree