Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Solution:
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode sortedArrayToBST(int[] num) { int N=num.length; if(N<1) return null; return myTree(num, 0, N-1); } private TreeNode myTree(int[] num, int low, int high) { // TODO Auto-generated method stub if(low>high) return null; int mid=(low+high)/2; TreeNode root=new TreeNode(num[mid]); root.left=myTree(num,low,mid-1); root.right=myTree(num, mid+1, high); return root; } }