今天回老家,这个周末看书多一点
将有序的数组转变为二叉搜索树
public static TreeNode areverse(int[] arr){
if(arr==null) return null;
return recore(arr,0,arr.length-1);
}
public static TreeNode recore(int[] arr, int start, int end) {
if (arr.length == 0)
return null;
TreeNode root;
System.out.println("start:"+start+" end:"+end);
if (start == end) {
root = new TreeNode(arr[start]);
root.left = null;
root.right = null;
} else {
int mid = (start + end) / 2;
//System.out.println(mid);
root = new TreeNode(arr[mid]);
root.left = recore(arr, start, mid - 1);
root.right = recore(arr, mid + 1, end);
}
return root;
}
网友评论