Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
解题思路:
首先要理解,什么叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然后就十分容易了,JAVA实现如下:
public TreeNode sortedArrayToBST(int[] nums) { return sortedArrayToBST(nums, 0, nums.length - 1); } static public TreeNode sortedArrayToBST(int[] nums, int begin, int end) { if (begin>end) return null; TreeNode root = new TreeNode(nums[(begin+end) / 2]); root.left=sortedArrayToBST(nums,begin,(begin+end) / 2-1); root.right=sortedArrayToBST(nums,(begin+end) / 2+1,end); return root; }