AC代码
public class Solution {
/*
* @param A: an integer array
* @return: A tree node
*/
public TreeNode sortedArrayToBST(int[] A) {
// write your code here
if (A.length == 0) {
return null;
}
return buildTree(A, 0, A.length-1);
}
public TreeNode buildTree(int[] A,int start,int end) {
if (start > end) {
return null;
}
int mid = (start+end) / 2;
TreeNode treeNode = new TreeNode(A[mid]);
treeNode.left = buildTree(A, start, mid-1);
treeNode.right = buildTree(A, mid+1, end);
return treeNode;
}
}
网友评论