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. 收放卷及张力控制 PID调试技巧

    1) 小 Kp( 0.01) , 大 Ti ( 20000ms) 2)逐渐增大Kp, 减小Ti ( 20000ms – 3000ms),避免发生震荡 3)观察I-out 是否在0附近 可能原因:卷径不 ...

  2. IOS 远程推送通知(UIRemoteNotification)

    ●  什么是远程推送通知 ●  顾名思义,就是从远程服务器推送给客户端的通知(需要联网) ●  远程推送服务,又称为APNs(Apple Push Notification Services) ●   ...

  3. HASH JION AND NESTED JION

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sssbbbryj/article/details/27795905  关于HASH_JION的原 ...

  4. POJ 3308 最少点集覆盖

    题意:和Uva 11419 类似. 首先最少点集覆盖 = 最大匹配. 我们可以在 S 和行 的边 不是1,有了权值,但是题意要求的是乘积最小,那么可以用 log(a*b) = loga + logb ...

  5. Nmap的基础知识

    扫描单一的一个主机: #nmap domain.com #nmap 192.168.1.2 扫描整个子网: #nmap 扫描多个目标: #nmap 192.168.1.2 192.168.1.5 查看 ...

  6. AQS2:可重入和阻塞

    本文仅基于可重入的锁(ReentrantLock类)对AQS做分析,只考虑独占锁. 共享锁与独占锁的更多信息,以后再讨论. AQS中队列的实现 节点Node AQS的节点包含了对前置节点的引用pre, ...

  7. Android学习笔记_40_系统结构 目录结构

    1.系统结构: 一.应用程序层 Android平台不仅仅是操作系统,也包含了许多应用程序,诸如SMS短信客户端程序.电话拨号程序.图片浏览器.Web浏览器等应用程序.这些应用程序都是用Java语言编写 ...

  8. bitmap和drawable的相互转化以及setImageResource(),setImageDrawable(),setImageBitmap()

    从本地获取drawable图片:getResources().getDrawable(R.drawable.**) 获取bitmap:Bitmap b=BitmapFactory().decodeRe ...

  9. 浅谈Java 8的新特性和使用场景

    一.default方法:   通过default方法,可以在接口(Interface interface_name)中添加实例化方法:   代码如下: public interface TestDef ...

  10. select()事件默认选中文本框的全部内容,并改变其背景色和文字颜色

    1.select()事件默认选中文本框的全部内容 拿到input标签的节点,调用select()方法即可.但是我做的vue项目中调用了此方法有一个bug,单次点击会全选内容,双次点击的时候全选会闪一下 ...