[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 ...
随机推荐
- Uva 10765 鸽子和炸弹
题目链接:https://vjudge.net/contest/166461#problem/B 题意: 给一个无向图,求每一个点删除后,剩下的连通块的数目: 分析: 只有割顶被删掉后,连通分量才会改 ...
- 【转】总结oninput、onchange与onpropertychange事件的用法和区别
经本人测试在chrome下的从历史记录中选取值的时候也户触发input事件 前端页面开发的很多情况下都需要实时监听文本框输入,比如腾讯微博编写140字的微博时输入框hu9i动态显示还可以输入的字数.过 ...
- HDU 5536 Chip Factory 【01字典树删除】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536 Chip Factory Time Limit: 18000/9000 MS (Java/Ot ...
- java json和对象互转
开发过程中遇到一些对象转string和string转对象的问题,浪费了很久,现在用的熟练些了,总结如下: 1.字符串尽量定义成json可解析的,如{"name":"a&q ...
- 【luogu P3366 最小生成树】 模板
这里是kruskal做法 当然prim也可以,至于prim和kruskal的比较: Prim在稠密图中比Kruskal优,Kruskal在稀疏图中比Prim优. #include<bits/st ...
- Notepad++正则表达式使用
推荐个正则表达式在线测试的工具http://ccmpp.com/Regex/ Notepad++正则表达式使用 -- ::| 分类: 文档 | 标签:正则表达式 替换 notepad++ 匹配 查找 ...
- 【Linux-CentOS】【转-更正】使用CentOS DVD1 和DVD2做本地yum源
原文在此.此文写的非常好,怕网络丢失,特转来,并做了更正. CentOS6以上版本一般都会提供一个DVD1和一个DVD2镜像,使用DVD1即可安装使用CentOS了,DVD2中存放了一些额外的软件包, ...
- mobileeye
if a human can drive a car based on vision alone – so can a computer. 但是目前哪家能做到?
- android 网络技术基础学习 (七)
使用httpclient协议访问网络: public class MainActivity extends Activity implements OnClickListener{ public vo ...
- Python 学习笔记(六)Python第一个程序
Python 语句 赋值语句 1.将3对象赋值给了变量a 2.将3,4赋值给了变量a,b >>> a = 3 >>> a ,b = 3,4 >>> ...