# 【Leetcode】Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

	public int getListLength(ListNode head) {
int length = 0;
while (p != null) {
length++;
p = p.next;
}
return length;
}

int i = 0;
while (p != null) {
n[i] = p.val;
i++;
p = p.next;
}
return sortedArrayToBST(n);
}

/**
* Convert Sorted Array to Binary Search Tree
*/
public TreeNode sortedArrayToBST(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
return test(nums, 0, nums.length - 1);
}

public TreeNode test(int[] nums, int left, int right) {
if (right < left)
return null;
int mid = (left + right) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = test(nums, left, mid - 1);
root.right = test(nums, mid + 1, right);
return root;
}


#### LeetCode --- 109. Convert Sorted List to Binary Search Tree

2015-04-17 16:48:22

#### Convert Sorted List to Binary Search Tree -- LeetCode

2014-04-17 07:57:02

#### LeetCode Convert Sorted List to Binary Search Tree 解题报告

2014-10-01 22:30:08

#### LeetCode(109)Convert Sorted List to Binary Search Tree

2014-01-19 10:53:55

#### 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree（排序链表转换成二叉排序树）】

2015-08-10 07:46:41

#### [Leetcode][python]Convert Sorted List to Binary Search Tree

2017-07-27 06:30:48

#### LeetCode(108)Convert Sorted Array to Binary Search Tree

2014-01-18 02:17:01

#### Convert Sorted Array to Binary Search Tree -- LeetCode

2014-04-17 07:56:34

#### LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解

2016-02-25 19:35:34

#### 108. Convert Sorted Array to Binary Search Tree Leetcode Python

2015-01-16 07:36:06