[Cracking the Coding Interview] 4.2 Minimal Tree 最小树
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 最小树的更多相关文章
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking The Coding Interview 4.6
//原文: // // Design an algorithm and write code to find the first common ancestor of two nodes in a b ...
- 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 ...
随机推荐
- Selenium2学习(三)-- 八种元素元素定位(Firebug和firepath)
前言 自动化只要掌握四步操作:获取元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告.本篇主要讲如何用firefox辅助工具进行元素定位.元素定位在这四个环节中是至关 ...
- 免费的SSL证书,你值得拥有!Let's Encrypt 试用体验记录
早上收到 Let’s Encrypt 的邮件,说偶之前申请的已经通过了,于是马上开始试用.Let’s Encrypt 是一个新的数字证书认证机构,它通过自动化的过程消除创建和安装证书的复杂性,为网站提 ...
- Oracle服务端及客户端搭建帮助文档
Oracle服务端及客户端搭建帮助文档 目录 简介 Oracle服务端安装 Oracle客户端安装 PLSQL安装 登录测试 系统配置修改 用户操作 解锁账户.密码 创建账户及密码 配置监听文件 监听 ...
- C++ double 小数精度控制
第一种方法:cout<<fixed<<setprecision(20)<<mydouble<<endl; #include <iostream&g ...
- libevent-signal(2)
上一节着重分析了event_init是如何集成signal,这一节从event_add展开分析 ev_events有四种类型 I/O事件: EV_WRITE和EV_READ 定时事件:EV_TIMEO ...
- 用C++实现HTTP服务器 - Windows平台(开放源代码)
有时间了看一下 https://blog.csdn.net/querw/article/details/6593328 libevent也实现了一下http服务
- 2016 ACM/ICPC亚洲区大连站 F - Detachment 【维护前缀积、前缀和、二分搜索优化】
F - Detachment In a highly developed alien society, the habitats are almost infinite dimensional spa ...
- 自定义Powershell提示符
实现效果: 实现原理: Powershell将个人配置脚本文件的地址存放在$profile变量中, 通过修改该变量达到想要的目的. 实现过程: 1>创建一个新的配置脚本: 2>编辑脚本内容 ...
- XCode项目配置可访问 非 https 接口的方法
打开项目的info.plist文件,右键- open as sourceCode .在代码中添加: <key>NSAppTransportSecurity</key> < ...
- Android学习笔记_74_Android回调函数触发的几种方式 广播 静态对象
一.通过广播方式: 1.比如登录.假如下面这个方法是外界调用的,那么怎样在LoginActivity里面执行登录操作,成功之后在回调listener接口呢?如果是平常的类,可以通过构造函数将监听类对象 ...