Convert Sorted Array to Binary Search Tree with Minimal Height
题目:
Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height.
分析:
基本题~
解法:
public class Solution {
/**
* @param A: an integer array
* @return: a tree node
*/
public TreeNode helper(int[] A, int start, int end) {
int mid = start + (end - start) / 2;
TreeNode root = new TreeNode(A[mid]);
if (start <= mid - 1) {
root.left = helper(A, start, mid - 1);
}
if (mid + 1 <= end) {
root.right = helper(A, mid + 1, end);
}
return root;
}
public TreeNode sortedArrayToBST(int[] A) {
// write your code here
if (A.length == 0) {
return null;
}
return helper(A, 0, A.length - 1);
}
}