108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
2/17/2017, Java
performance不好
1 public class Solution { 2 public TreeNode sortedArrayToBST(int[] nums) { 3 if (nums == null) return null; 4 return sortedArrayToBST(nums, 0, nums.length - 1); 5 } 6 private TreeNode sortedArrayToBST(int[] nums, int lo, int hi) { 7 if (lo > hi) return null; 8 int mid = lo + (hi - lo) / 2; 9 TreeNode ret = new TreeNode(nums[mid]); 10 ret.left = sortedArrayToBST(nums, lo, mid - 1); 11 ret.right = sortedArrayToBST(nums, mid + 1, hi); 12 return ret; 13 } 14 }