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. 用PHP向数据库中实现简单的增删改查(纯代码)

    <?php $con = mysql_connect("localhost:3306","root",""); if (!$con) ...

  2. mongorc.js文件

    当启动的时候,mongo检查用户HOME目录下的一个JavaScript文件.mongorc.js.如果找到,mongo在首次显示提示信息前解析.mongorc.js的内容.如果你使用shell执行一 ...

  3. 关于APIT定位算法的讨论

    关于APIT定位算法的讨论 [摘要]   无线传感器网络节点定位机制的研究中,基于距离无关的定位技术得到快速发展,其中基于重叠区域的APIT定位技术在实际环境中的定位精度高,被广泛研究和应用. [关键 ...

  4. react中PropTypes与DefaultProps的应用

    每个组件都有自己的props参数,这参数是从父组件接收的一些属性,那么如何对参数的类型作校验.如何定义参数的默认值.这里涉及到两个基础的概念,叫做proptypes 和 defaultprops.子组 ...

  5. 行高 line-height

    一.行高的定义 line-height(行高):两行文字基线之间的距离 1.什么是基线? 2.为何是基线? 3.需要两行吗? 1.什么是基线? 我们上学的时候都用过,抄写英文字母的时候.其中有一条红线 ...

  6. c++字符串初始化

    #include<string> string s1 = "abcdefg"; string s2("abcdefg");

  7. 【luogu T34117 打油门】 题解

    王强怎么这么强啊 王强太强了 二维树状数组 #include <cstdio> #include <cstring> #include <iostream> #in ...

  8. while、dowhile、switchcase 循环嵌套、穷举、迭代

    for(var i=0;i<5;i++)//假如规定到5 { document.write("123456<br/>") } 如果变成这样 var i=0: fo ...

  9. c语言描述的双向链表的基本操作

    #include<stdio.h> #include<stdlib.h> #define ok 1 #define error 0 typedef int Status; ty ...

  10. 【转】RMAN删除过期备份或非过期备份

    (一)删除备份--DELETE命令用于删除RMAN备份记录及相应的物理文件.当使用RMAN执行备份操作时,会在RMAN资料库(RMAN Repository)中生成RMAN备份记录,默认情况下RMAN ...