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. 【Spring实战】—— 10 AOP针对参数的通知

    通过前面的学习,可以了解到 Spring的AOP可以很方便的监控到方法级别的执行 ,针对于某个方法实现通知响应. 那么对于方法的参数如何呢? 比如我们有一个方法,每次传入了一个字符串,我想要知道每次传 ...

  2. day3-购物车小程序

    1.要求 启动程序后,让用户输入工资,然后打印商品列表 允许用户genuine商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒还有多少 可随时退出,退出时,打印已购买商品和余 ...

  3. Orchard Core 使用模板创建Module

    根据官方示例:https://orchardcore.readthedocs.io/en/latest/Templates/README/#create-a-new-module 执行以下命令: do ...

  4. 模拟误删除InnoDB ibdata数据文件恢复

    注意:假如误删除 ibdata文件 ,此时千万别把mysqld进程杀死,否则没法挽救. 1.模拟删除ibdata数据文件和重做日志文件: [root@hcdb0 data]# lltotal 4219 ...

  5. [19/03/21-星期四] 异常(Exception) (一)

    一.引言 在实际工作中,我们遇到的情况不可能是非常完美的.比如:你写的某个模块,用户输入不一定符合你的要求;你的程序要打开某个文件, 这个文件可能不存在或者文件格式不对 ,你要读取数据库的数据,数据可 ...

  6. print (re.findall("(?:abc)+","abcabcabc"))

    _*_ coding:utf-8 _*_ import re findall 有括号优先级,所以我们这里一直出现的都是 abc print (re.findall("(abc)+" ...

  7. Windows7安装Envi4.8简体中文破解版

    在正式安装前,建议先完整阅读本教程!本教程所使用的是Envi 4.8 32 位安装包,径测试,在64位windows7上可以正常安装使用!本教程就是在64位windows7上安装32位Envi4.8! ...

  8. oracle 基本语法(一)

    1.基本语句: .查询每个部门工资最高的人的详细记录 select * from emp e,(select max(sal) max,deptno from emp group by deptno) ...

  9. Unity3d-制作粒子光环特效

    http://blog.csdn.net/ozhangseno/article/details/70799611

  10. Java 加密Excel文件(打开时需输入密码)

    收集:author: lifq package com.XXX.XXX.utils; import java.io.IOException; import com.jxcell.CellExcepti ...