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. [VMware]设置VM虚拟机随系统自动启动

    设置步骤: 1.找到VM的安装路径,右键vmware发送到桌面快捷方式 2.右键桌面快捷方式的属性,看到目标的属性框 3.找到需要自启动的虚拟机路径,如: D:\QC_VM\Clone of Wind ...

  2. [转]个人源码管理:如何在本机配置自己的SVN Repository (图解)

    本文转自:http://blog.csdn.net/wikijava/article/details/6245588 Repository 即源码的集中存放处,所有修改后提交的源码就是保存在这里,并在 ...

  3. JavaScript Patterns 4.5 Immediate Functions

    The immediate function pattern is a syntax that enables you to execute a function as soon as it is d ...

  4. Google自定义搜索引擎

    本文主要介绍如何通过Google的API来定义自己的搜索引擎,并将Google搜索框嵌入到自己的web页面.另外,分析了自定义搜索引擎请求数据的url,模拟请求并获取搜索的结果. 1 写在前面 前段时 ...

  5. HDU 4043 FXTZ II (组合数学-排列组合)

    FXTZ II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  6. js实现(全选)多选按钮

    第一种,全部选中: <html> <head> <title>复选框checked属性</title> <script language=&quo ...

  7. Python 元组知识点

    1.元组是一个有序的集合,2.元组和列表一样可以使用索引.切片来取值.3.创建元组后不能在原地进行修改替换等操作.4.元组支持嵌套,可以包含列表.字典和不同元组.5.元组支持一般序列的操作,例如:+. ...

  8. java 基础

    一 4类8种基本数据类型 逻辑型   - boolean 文本型   - char 整数型   - byte short int long 浮点数型 - float double ☆java各整数类型 ...

  9. VMware Workstation不可恢复的错误:(vmui)

    虚拟机中部署项目,由于项目的日志是gbk的,就把虚拟机中linux编码改成gbk了,结果问题来了,日志显示中文正常了,但是虚拟机运行一下就出错了,注意虚拟机出错,并没导致linux也挂掉,只是linu ...

  10. git一些常用设置

    用法:git config [选项] 配置文件位置    --global              使用全局配置文件    --system              使用系统级配置文件    -- ...