我试图找到数组的中间元素或索引,然后得到每一半的中间,依此类推。。。
假设我们有
[0,1,2,3,4,5,6,7,8,9,10,11,12,13]
我所期待的
7, 3, 1, 0, 2, 5, 4, 6 ...
这样,当我从新数组中添加元素时,我最终得到了一个平衡的BST
-
开始:开始索引(0)
-
结束:长度-1
-
nums:要添加的数字列表
-
b: 将插入到的树
代码:
public static BST fillBST(BST b, List<Integer> nums, int start, int end) {
int mid = (start + end) / 2;
if (start > end)
b.insertt(nums.get(mid));
else {
fillBST(b, nums, start, mid - 1);
fillBST(b, nums, mid + 1, end);
}
return b;
}
输出
我使用列表获取
[0,31]: 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31