Implement a function to check if a binary tree is balanced. For the purpose of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.

这道题让我们检查一棵树是不是平衡的,根据题目平衡二叉树的定义我们可以对于每个node递归的检测一下它的substrees是否符合这个条件。见如下的代码:

height函数递归的计算出一棵树的高度,is_balanced?函数递归的判断每个node的子树是不是平衡。

class TreeNode
attr_accessor :val, :left, :right def initialize(val)
@val = val
@left = nil
@right = nil
end
end def is_balanced?(root)
return true if root.nil? if (height(root.left) - height(root.right)).abs <= 1
return is_balanced?(root.left) && is_balanced?(root.right)
else
return false
end
end def height(root)
return 0 if root.nil? left_height = height(root.left)
right_height = height(root.right) return [left_height, right_height].max + 1
end

[Cracking the Coding Interview] 4.4 Check Balanced的更多相关文章

  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 4.1

    //Implement a function to check if a tree is balanced. For the purposes of this question, a balanced ...

  8. Cracking The Coding Interview 1.8

    //Assume you have a method isSubstring which checks if one word is a substring of another. //Given t ...

  9. Cracking the Coding Interview(String and array)

    1.1实现一个算法判断一个字符串是否存在重复字符.如果不能利用另外的数据结构又该如何实现? My solution: /** *利用类似一个hash table的计数 *然后检查这个hash tabl ...

随机推荐

  1. create-react-app 打包后文件路径问题

    在package.json 加入 "homepage": "需要的路径",

  2. O/S-Error: (OS 23) Data error (cyclic redundancy check)问题处理

    RMAN-03002: backup plus archivelog 命令 (在 08/24/2015 03:31:00 上) 失败ORA-19501: 文件 "XXXXXX.DBF&quo ...

  3. RAC共享磁盘挂载工具

    待更新: oracleasm: udev: 自己搭建个存储服务器:

  4. TP5.0:跳转链接到某控制器下的某方法

    语法:{:url('控制器名/方法名')} 例如: 结果: 以上 END

  5. IOS Xib使用

  6. IOS6 的特性 及 autoalyout的作用

    1.如果控件有默认的内容(宽高), 我们只需设置autoalyout的X/Y, autolayout会自动计算出宽高 2.Xcode6将Storyboard变成豆腐干的目的:在Xcode6之前, 如果 ...

  7. 【转】Activity、Window、View的关系

    1.先看一个现象 1 2 3 4 5 6 7 8 9 10 11 public class MainActivity extends Activity {       @Override     pr ...

  8. 2018.10.2 Eclipse中如何测地修改一个we项目步骤

    找到项目的web.xml文件 大概的路径如下: 修改xml文件中的display-name 节点的值 下一步就是切换工作目录 显示的效果 打开最后一个文件修改 接下来找到这个文件 是部署的时候用的 运 ...

  9. Spring多个版本源码地址分享

    源码地址为:http://repo.spring.io/simple/libs-release-local/org/springframework/spring/,以供研究源码的朋友. 我看了好几本关 ...

  10. 关于eclipse中引入项目报错或者没有JRE System Library问题(jre报错)或者jre1.7(8)改为jre1.8(7)等问题

    解决方法: 右键项目工程-->>properties->>java bulid path -->>>libraries -->>add libra ...