1. 程式人生 > >給定有序數組,創建高度最小的二叉查找樹

給定有序數組,創建高度最小的二叉查找樹

enter reat 技術 二叉查找樹 treenode ret new t pre 有序數組

技術分享

技術分享


TreeNode createMinimalBST(int arr[], int start, int end)

{
if (end < start)
{
return null;
}
int mid = start + (end - start) / 2;
TreeNode n=new TreeNode(arr[mid]);
n.left=createMinimalBST(arr,start,mid-1);
n.right=createMinimalBST(arr,mid+1,end);
return n;
}
TreeNode createMinimalBST(int array[])
{
return createMinimalBST(array,0,array.length-1);
}

給定有序數組,創建高度最小的二叉查找樹