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

这道题给了我们一个从小到大排好序的元素不重复的数组,让我们构建一个有最小高度的二分查找树。有两个限制条件,二分查找和最小高度。我们先来看如何树的高度最小,这里就要涉及到平衡二叉树的概念。我们要保证每个node的左右子树的高度差绝对值不超过1。第二个限制条件是这个树是二分查找树,也就是要保证每个node的值大于等于其左子树的最大值,小于其右子树的的最小值。如何根据这两个条件构建一颗minimal tree呢?我们一开始可以从数组中取中间的值作为root,然后将数组一分为二,然后用同样的方法对左半部分和右半部分做同样的递归操作。见如下代码:

class Node
attr_accessor :val, :left, :right def initialize(val)
@val = val
@left = nil
@right = nil
end
end def build(nums)
return nil if nums.nil? || nums.empty? mid = nums.length / 2 root = Node.new(nums[mid]) left = build(nums[0...mid])
right = build(nums[mid + 1..-1]) root.left = left
root.right = right
root
end root = build([1,2,3,4,5,6]) # inorder traverse一下树来验证一下
def inorder_traversal(root)
return if root.nil? inorder_traversal(root.left)
puts root.val
inorder_traversal(root.right)
end inorder_traversal(root)

[Cracking the Coding Interview] 4.2 Minimal Tree 最小树的更多相关文章

  1. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  2. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  3. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. Cracking The Coding Interview 4.6

    //原文: // // Design an algorithm and write code to find the first common ancestor of two nodes in a b ...

  9. Cracking The Coding Interview 4.4

    //Given a binary search tree, design an algorithm which creates a linked list of all the nodes at ea ...

随机推荐

  1. WAKE-SPM-综述

    1,SPM 1,1source paper:http://lear.inrialpes.fr/pubs/2007/ZMLS07/ZhangMarszalekLazebnikSchmid-IJCV07- ...

  2. 【Spring实战】—— 13 AspectJ注解切面

    前面了解了典型的AOP基于配置的使用方法,下面介绍下如何依赖于注解来实现AOP. 基于注解降低了配置文件的复杂程度,但是引入了程序间的耦合,其中的优劣待用户自己判断了. 需要注意的是,确定Aspect ...

  3. CSS z-index的用法

    理清 position及z-index的用法: static :  无特殊定位,对象遵循HTML定位规则absolute :  将对象从文档流中拖出,使用left,right,top,bottom等属 ...

  4. 学习和运用scrum

    作为长大的大三老腊肉,我们已经在长大生活了两年多,对于什么是长大人最想完善的校园需求.最想拥有的校园服务媒介也有了更加深切的体会. 于是,GoodJob小团队blingbling闪现啦!! GoodJ ...

  5. 找出OData service出错根源的小技巧

    SAP的Fiori应用是通过OData和后台交互的.在使用Fiori应用时您可能会遇到这样的错误消息: 这个错误消息没有包含有助于partner或者客户定位问题根源的线索. 下面是如何在后台找出问题根 ...

  6. MHA 一主一从搭建

    安装基本环境介绍 一主一从     MHA manager & slave  node2 192.168.56.27 Master机器 node1 192.168.56.26 VIP   19 ...

  7. POJ 3308 最少点集覆盖

    题意:和Uva 11419 类似. 首先最少点集覆盖 = 最大匹配. 我们可以在 S 和行 的边 不是1,有了权值,但是题意要求的是乘积最小,那么可以用 log(a*b) = loga + logb ...

  8. 【洛谷P1107】 [BJWC2008]雷涛的小猫

    雷涛的小猫 题目链接 n^2DP比较好想, f[i][j]表示第i棵树高度为j的最大收益 直接从上到下转移即可,每次记录下max f[1~n][j] 用于下面的转移 f[i][j]=max(f[i][ ...

  9. 【P2447 [SDOI2010]外星千足虫】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2447 dalao们都说简单...解异或方程组 可我不是dalao qwq #include <algo ...

  10. springboot2.04+mybatis-plus+swagger2+CodeGenerator

    @author zhangyh SpringBoot技术栈搭建个人博客[项目准备]  RESTful API就是一套协议来规范多种形式的前端和同一个后台的交互方式 原型设计 事实上,我是直接先去找的原 ...