程序员面试金典-高度最小的BST
题目描述
对于一个元素各不相同且按升序排列的有序序列,请编写一个算法,创建一棵高度最小的二叉查找树。
给定一个有序序列int[] vals,请返回创建的二叉查找树的高度。
class MinimalBST { public: int buildMinimalBST(vector<int> vals) { // write code here int len = vals.size(); if(len == 0){ return 0; } int cnt = 1, d = 1, idx = 2; while(len > cnt){ cnt += idx; idx = 2*idx; d++; } return d; } };