给定一个元素各不相同的有序序列int[] vals(升序排列),请编写算法创建一棵高度最小的二叉查找树,并返回二叉查找树的高度。
//思路:取有序数组中间数值作为二叉搜索树的根,这样高度最小。确定根之后,递归依次确定根的左子树的根,右子树的根。。。 /*struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int data) :val(data),left(NULL),right(NULL) {} };*/ class MinimalBST { public: TreeNode *buildBST(vector<int> vals,int left,int right) { if (left > right) return NULL; int middle = left + (right - left)/2; TreeNode *root = new TreeNode(vals[middle]); root->left = buildBST(vals,left,middle-1); root->right = buildBST(vals,middle+1,right); return root; } int highBST(TreeNode *root) { if (root == NULL) return 0; int left = highBST(root->left); int right = highBST(root->right); if (left > right) return left + 1; else return right + 1; } int buildMinimalBST(vector<int> vals) { // write code here int length = vals.size(); if (length <= 0) return 0; TreeNode *root = buildBST(vals,0,length-1); return highBST(root); } };
import java.util.*; public class MinimalBST { public int build(int[] vals, int start, int end) { if (end <= start) { return 1; } int mid = (start + end) >> 1; int height1 = 1 + build(vals, start, mid - 1); int height2 = 1 + build(vals, mid + 1, end); return Math.max(height1, height2); } public int buildMinimalBST(int[] vals) { // write code here if (vals == null || vals.length < 1) return 0; return build(vals, 0, vals.length - 1); } }
//虽然可以直接公式算高度,不过也写了个建树顺便算高度的 class MinimalBST { public: int buildMinimalBST(vector<int> vals) { int height = 0;//初始化 buildMinimalBST(vals, 0, vals.size() - 1, height); return height; } TreeNode* buildMinimalBST(vector<int> vals, int start, int end, int& height){ if(start > end){//递归终止条件 height = 0; return NULL; } int mid = start + (end - start) / 2; TreeNode *root = new TreeNode(vals[mid]);//根节点 int left, right;//左右子树 root->left = buildMinimalBST(vals, start, mid - 1, left);//左子树 root->right = buildMinimalBST(vals, mid + 1, end, right);//右子树 height = (left >= right ? left : right) + 1;//计算当前高度 return root; } };
import java.util.*; public class MinimalBST { public int buildMinimalBST(int[] vals) { // write code here if(vals.length == 0){ return 0; } int length = vals.length; TreeNode node = build(vals,0,length - 1); return max(node); } public TreeNode build(int[] vals,int left,int right){ if(left > right){ return null; } int mid = left + (right - left)/2; TreeNode root = new TreeNode(vals[mid]); root.left = build(vals,left,mid - 1); root.right = build(vals,mid+1,right); return root; } public int max(TreeNode root){ if(root == null){ return 0; } int left = max(root.left) + 1; int right = max(root.right) + 1; return Math.max(left,right); } }
class MinimalBST { public: int buildMinimalBST(vector<int> vals) { if (vals.size() == 0) return 0; return log2(vals.size()) + 1; } };
class MinimalBST: def buildMinimalBST(self, vals): # write code here if not vals: return 0 return self.build(vals,0,len(vals)-1) def build(self,vals,start,end): if end<start:return 0 if end==start:return 1 mid = start + (end - start) / 2 height1 = 1+self.build(vals,start,mid-1) height2 = 1+self.build(vals,mid+1,end) return max(height1,height2)
class MinimalBST { private: TreeNode* build(vector<int>& vals, int l, int r) { if(l > r) return NULL; int mid = l + (r-l)/2; TreeNode* head = new TreeNode(vals[mid]); head->left = build(vals,l,mid-1); head->right = build(vals,mid+1,r); return head; } int depth(TreeNode* root) { if(!root) return 0; return max(depth(root->left),depth(root->right))+1; } public: int buildMinimalBST(vector<int> vals) { TreeNode* root = build(vals,0,vals.size()-1); return depth(root); } };
//当然最简单方法就是返回log2(vals.size())+1 class MinimalBST { struct TreeNode { int val; TreeNode *left,*right; TreeNode(int v):val(v),left(nullptr),right(nullptr){} }; public: int buildMinimalBST(vector<int> vals) { if(vals.size()<=0) return 0; int size=vals.size(); TreeNode *root=CreateBST(vals,0,size-1); return GetHeight(root); } private: TreeNode* CreateBST(vector<int>&arr,int left,int right) { if(left>right) return nullptr; int mid=left+(right-left)/2; TreeNode *root=new TreeNode(arr[mid]); root->left=CreateBST(arr,left,mid-1); root->right=CreateBST(arr,mid+1,right); return root; } int GetHeight(TreeNode *root) { if(root==nullptr) return 0; return max(GetHeight(root->left),GetHeight(root->right))+1; } };
class MinimalBST { public: int buildMinimalBST(vector<int> vals) { int h = 0; helper(vals, 0, vals.size()-1, 0, h); return h; } private: TreeNode* helper(const vector<int>& vals, int s, int e, int cnt, int& h){ if(s > e) return NULL; int mid = s + (e-s)/2; TreeNode* node = new TreeNode(vals[mid]); if(++cnt > h) h = cnt; node->left = helper(vals, s, mid-1, cnt, h); node->right = helper(vals, mid+1, e, cnt, h); return node; } };
import java.util.*; public class MinimalBST { public int buildMinimalBST(int[] vals) { // write code here return build(vals, 0, vals.length - 1); } private int build(int[] vals, int start, int end) { if (end < start) { return 0; } else { int mid = (start + end) / 2; int leftHeight = build(vals, start, mid - 1); int rightHeight = build(vals, mid + 1, end); return Math.max(leftHeight, rightHeight) + 1; } } }
import java.util.*; public class MinimalBST { public int buildMinimalBST(int[] vals) { if(vals == null || vals.length <1) return 0; return buildMinimalBSTCore(vals, 0 , vals.length-1); } public int buildMinimalBSTCore(int[] vals, int start, int end) { if(end < start) return 0; int mid = (start + end)/2; int leftH = 1 + buildMinimalBSTCore(vals, start, mid-1); int rightH = 1 + buildMinimalBSTCore(vals, mid+1, end); return Math.max(leftH, rightH); } }