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

1.Divide the array equally into left part and right part, the mid value will be the root.

2.Recall the function to the left and right part of the array.

def array_to_tree(a)
to_tree(a,0,a.length-1)
end def to_tree(a,s,e)
return if s > e
n = treeNode.new(a[(s+e)/2])
n.left, n.right = to_tree(a,s,(s+e)/2-1), to_tree(a,(s+e)/2+1,e)
n
end

Cracking the Code Interview 4.3 Array to Binary Tree的更多相关文章

  1. Cracking the code interview

    推荐一本书<Cracking the code interview> Now in the 5th edition, Cracking the Coding Interview gives ...

  2. 【读书笔记】Cracking the Code Interview(第五版中文版)

    导语 所有的编程练习都在牛客网OJ提交,链接: https://www.nowcoder.com/ta/cracking-the-coding-interview 第八章 面试考题 8.1 数组与字符 ...

  3. 【Cracking the Code Interview(5th edition)】二、链表(C++)

    链表结点类型定义: class Node { public: ; Node *next = nullptr; Node(int d) { data = d; } }; 快行指针(runner)技巧: ...

  4. 【Cracking the Code Interview(5th edition)】一、数组与字符串(C++)

    1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.不允许使用额外的数据结构. 解答:这里假定字符集为ASCII码,可以与面试官沟通确认字符串使用的字符集.由于字符集是有限的,建立一个数组模拟 ...

  5. Cracking the Coding Interview(Trees and Graphs)

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

  6. 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 ...

  7. Cracking the coding interview

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

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

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

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

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

随机推荐

  1. 机器人学 —— 轨迹规划(Configuration Space)

    之前的轨迹规划中,我们只考虑了质点,没有考虑机器人的外形与结构.直接在obstacle map 中进行轨迹规划,然而世纪情况中,机器人有固定外形,可能会和障碍物发生碰撞.此情况下,我们针对机器人自由度 ...

  2. android ADB命令的使用

    http://jingyan.baidu.com/article/fcb5aff7f55c63edab4a7174.html 综上所述,我觉得告知各位菜鸟同学如何删除自带的程序是很有必要的一件事情.1 ...

  3. 【玩转Ubuntu】09. Ubuntu上安装apktool

    下载两个文件 到这里 https://code.google.com/p/android-apktool/downloads/list?q=label:Featured下载这个文件  1.  apkt ...

  4. ado执行sql查询出现“发送数据流时出现算术溢出”错误

    开发一个数据采集监控系统,比较变态的是有将近2000项数据.根据数据类型分多个表存储.数据库访问层采用ado.最近发现当一条sql一次性查询1700多个字段数据后就出现“发送数据流时出现算术溢出”错误 ...

  5. linux下c程序调用reboot函数实现直接重启【转】

    转自:http://www.blog.chinaunix.net/uid-20564848-id-73878.html linux下c程序调用reboot函数实现直接重启 当然你也可以直接调用syst ...

  6. django中的filter详解

    filter (数据过滤) 我们很少会一次性从数据库中取出所有的数据:通常都只针对一部分数据进行操作. 在Django API中,我们可以使用`` filter()`` 方法对数据进行过滤: > ...

  7. android boot.img 结构

    android 的boot.img 包括 boot header,kernel, ramdisk 首先来看看Makefile是如何产生我们的boot.img的: boot镜像不是普通意义上的文件系统, ...

  8. 第十篇 PO核心功能及流程详解

    详见链接:http://bbs.erp100.com/thread-272866-1-1.html1. P2P lifecycleP2P是procure to pay的缩写,p2p循环值得就是采购到付 ...

  9. leetcode:Intersection of Two Linked Lists(两个链表的交叉点)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  10. 浅谈javascript中的作用域

    首先说明一下:Js中的作用域不同于其他语言的作用域,要特别注意     JS中作用域的概念: 表示变量或函数起作用的区域,指代了它们在什么样的上下文中执行,亦即上下文执行环境.Javascript的作 ...