4.3 Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.

这道题给了我们一个有序的数组,让我们来生成一个最小高度的二叉搜索树,为了达到最小高度,肯定是尽可能的填成一个满二叉树,左子树填满,右子树尽可能的填满。而且要注意是二叉搜索树,左<根<右的性质不能忘。既然给了我们一个有序的数组,那么我们可以取中间的数字为根节点,然后左半段为左子树,右半段为右子树,然后再递归去分别再分,有点像二叉搜索法的原理,代码不复杂,也不难懂,如下所示:

class Solution {
public:
TreeNode* createMinimalBST(vector<int> &nums) {
return createMinimalBST(nums, , nums.size() - );
}
TreeNode* createMinimalBST(vector<int> &nums, int start, int end) {
if (start > end) return NULL;
int mid = (start + end) / ;
TreeNode *node = new TreeNode(nums[mid]);
node->left = createMinimalBST(nums, start, mid - );
node->right = createMinimalBST(nums, mid + , end);
return node;
}
};

[CareerCup] 4.3 Create Minimal Binary Search Tree 创建最小二叉搜索树的更多相关文章

  1. LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  2. LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)

    题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...

  3. LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  4. LeetCode OJ:Validate Binary Search Tree(合法的二叉搜索树)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  6. [LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆

    描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...

  7. [leetcode]109. Convert Sorted List to Binary Search Tree链表构建二叉搜索树

    二叉树的各种遍历方式都是可以建立二叉树的,例如中序遍历,就是在第一步建立左子树,中间第二步建立新的节点,第三步构建右子树 此题利用二叉搜索树的中序遍历是递增序列的特点,而链表正好就是递增序列,从左子树 ...

  8. 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

    [096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...

  9. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

随机推荐

  1. Mac平台下启动MySQL到完全终止MySQL----终端八步走

    1.选中Finder的情况下,快捷键进入搜索目录:/usr/local 然后进入mysql目录下: 2.右键 "从这里启动" 打开终端: 3.输入执行:./scripts/mysq ...

  2. jQuery Validate 表单验证插件----利用jquery.metadata.js将校验规则直接写在class属性里面并定义错误信息的提示

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二. 添加一个另外一个插件jquery.metadata.js 并把校验规则写在控件里面 ...

  3. sudo简单命令语法及配置

    参考:http://yangrong.blog.51cto.com/6945369/1289452, https://wiki.archlinux.org/index.php/Sudo_(%E7%AE ...

  4. LAMP编译参数查看

    Linux下查看Nginx.Napache.MySQL.PHP的编译参数的命令如下: 1.nginx编译参数:#/usr/local/nginx/sbin/nginx -V2.apache编译参数:# ...

  5. Solr Dataimporthandler 导入MySQL 内存溢出。

    最近准备把一千九百多万数据导入Solr中,在以前测试数据只有一两百万,全量导入没有任务问题.但是,换成一千九百万数据时,solr报内存异常(java.lang.OutOfMemoryError:GC ...

  6. 树形DP codevs 1814 最长链

    codevs 1814 最长链  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中 ...

  7. C# 使用NLog记录日志

    NLog是一个记录日志组件,和log4net一样被广泛使用,它可以将日志保存到文本文件.CSV.控制台.VS调试窗口.数据库等.最近刚用到这个组件,觉得不错,水一篇. 下载 通过Nuget安装NLog ...

  8. codeforces 721C C. Journey(dp)

    题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  9. IUYYLIUIU

    #include<cstdio> using namespace std; int main() { printf("IIIIU !"); ; } //http://c ...

  10. [AS3] 问个很囧的问题: 如何遍历Dictionary?

    可以使用 for...in 循环或 for each...in 循环来遍历 Dictionary 对象的内容. for...in 循环用于基于键进行遍历: 而 for each...in   循环用于 ...