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. SpringMvc-helloword

    说明:在此只说明helloword的简单实现,通过helloword例子先了解springMvc是这样工作的,然后在一步步的研究原理 配置web.xml 1.配置servlet servlet-cla ...

  2. ubuntu安装google test

    google test 简称gtest,是一个C/C++的单元测试框架,它的代码在github仓库,使用起来还是挺方便的. 安装 先确保PC上有安装cmake: sudo cmake --versio ...

  3. 团队的初体验与Scrum的初识

    一. 队名及宣言 队名: the better for you 宣言: Change our lives with code 二. 队员及分工 a.承担软件工程的角色 姓名 学号 角色 蒋 婷 B20 ...

  4. python:进程操作

    一.多进程应用 import socket from multiprocessing import Process def talk(conn): conn.send(b'connected') re ...

  5. CFS调度分析(内核版本:2.6.34)

    CFS调度分析(内核版本:2.6.34) 1.时间记账 CFS不再有时间片的概念,他维护的是每个进程运行的时间记账 使用调度器实体结构来追踪进程运行记账: <linux/sched.h> ...

  6. (第三场) H Diff-prime Pairs 【数论-素数线性筛法+YY】

    题目链接 题目描述 Eddy has solved lots of problem involving calculating the number of coprime pairs within s ...

  7. [18/12/01]super 关键字和final 关键字

    一.super 关键字 1.super是直接父类对象的引用.可以通过super来访问父类中被子类覆盖的方法或属性. 使用super调用普通方法,语句没有位置限制,可以在子类中随便调用. 代码示例: c ...

  8. Django学习之ORM操作

    一.一般操作 二.必知必会13条 返回QuerySet对象的方法有 特殊的QuerySet 返回具体对象的 返回布尔值的方法有 返回数字的方法 三.单表查询之神奇的双下划线 四.ForeignKey操 ...

  9. 【luogu P3063 [USACO12DEC]牛奶的路由Milk Routing】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3063#sub 我很好奇这道题为什么没被收入SPFA好题 #include <cstdio> #i ...

  10. JavaScript面向对象编程之创建对象

    参考资料依旧<JavaScript高级程序设计>,不得不说这本书写的太好了,讲的极为清晰凝练,好书! 先给出重点笔记,好好理解下面的三条笔记,每一句话都很重要: 1.实例的指针仅指向原型, ...