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. JavaMelody监控spring、struts

    前言 前面讲过了Javamelody的基本配置,如何使用Javamelody来监控JDBC以及SQL. 这里继续讲解一下如何监控struts和spring. 手码不易,转载请注明:xingoo 由于s ...

  2. CentOS7 查询已安装的包方便整理 /卸载

    以PHP为例.首先查看已安装的PHP rpm -qa |grep php 输出结果: php-cli--.el7.x86_64 php-common--.el7.x86_64 查询rpm包的安装时间和 ...

  3. 给于用户Agent权限设置

    问题:有一个用户需要有create\alter\drop job人权限.默认是只有sysadmin成员才有这个权限.肯定不能将用户放到这个组 答案:所有JOB都属于msdb库中读取和写入信息.所以,肯 ...

  4. python:生成器进阶

    1,列表推导式 值 for 循环 2,生成器表达式 g=(i for i in range(10)) print(g) for i in g: print(i) 3,列表推导式与生成器表达式的区别 # ...

  5. 【洛谷2624】[HNOI2008] 明明的烦恼(Python+利用prufer序列结论求解)

    点此看题面 大致题意: 给你某些点的度数,其余点度数任意,让你求有多少种符合条件的无根树. \(prufer\)序列 一道弱化版的题目:[洛谷2290][HNOI2004] 树的计数. 这同样也是一道 ...

  6. UOJ 35 后缀数组

    后缀数组裸题,求排名第1~n的后缀,想相邻后缀的最长公共前缀. 集训队模板就是硬lO(∩_∩)O哈哈~ #include <cstdio> #include <cmath> # ...

  7. 2018.12.22 Spring学习02

    Spring学习02 1.使用注解配置Spring 1.1 为主配置文件引入新的命名空间(约束) 添加约束文件xxx-xxx-context.xml 添加到主配置文件中 选择刚才的context.xm ...

  8. 如何从GitHub获取源代码

    如何从GitHub获取源代码 github是当前流行的开源项目托管网站,里面有成千上万的项目值得学习和借鉴,可以把项目源代码下载到本地研究.本文介绍如何获取github的源代码. 方法1 - 克隆(C ...

  9. 【洛谷P1159】排行榜

    排行榜 题目链接 看到题解中一个很巧妙的做法: 先确定SAME的位置, 将DOWN的按输入顺序从上往下输出 再将UP的接着从上往下输出 这样便可以保证DOWN的人名次一定下降了 UP的人名次一定上升了 ...

  10. 【luogu P1993 小K的农场】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1993 1.差分约束: 对于a - b <= c 有一条 b-->a 权值为c 对于a - b & ...