Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
balanced tree 是指 tree的 min depth 和max depth不超过某个数值(1)。而不是完全二叉树!!!!
1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public TreeNode sortedArrayToBST(int[] num) {12 // IMPORTANT: Please reset any member data you declared, as13 // the same Solution instance will be reused for each test case.14 return buildTree(num, 0, num.length - 1);15 }16 public TreeNode buildTree(int[] num, int s, int e){17 if(s > e) return null;18 int mid = (s + e) / 2;19 TreeNode root = new TreeNode(num[mid]);20 root.left = buildTree(num, s, mid - 1);21 root.right = buildTree(num, mid + 1, e);22 return root;23 }24 }