[Cracking the Coding Interview] 4.4 Check Balanced
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的更多相关文章
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking The Coding Interview 4.1
//Implement a function to check if a tree is balanced. For the purposes of this question, a balanced ...
- Cracking The Coding Interview 1.8
//Assume you have a method isSubstring which checks if one word is a substring of another. //Given t ...
- Cracking the Coding Interview(String and array)
1.1实现一个算法判断一个字符串是否存在重复字符.如果不能利用另外的数据结构又该如何实现? My solution: /** *利用类似一个hash table的计数 *然后检查这个hash tabl ...
随机推荐
- ZT onActivityResult在android中的用法
onActivityResult在android中的用法 举例说我想要做的一个事情是,在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就 ...
- Markdown 学习
一级标题 一级标题 === 或 # 一级标题 二级标题 二级标题 --- 或 ## 二级标题 三级标题 ### 三级标题 链接Gayhub [Gayhub](https://www.github.co ...
- 使用DOM Breakpoints找到修改属性的Javascript代码
使用Chrome开发者工具的DOM断点功能可以让您快速找到修改了某一个DOM元素的Javascript代码. 在Chrome开发者工具里,选中想要监控的DOM元素,点击右键,选择Break on-&g ...
- MAVEN本地下载、安装
1. 安装jdk (此处版本选择1.7以上) 此处不做安装说明!!! 2.Eclipse.配置及安装 此处不做安装说明!!! 3.maven安装包(此处选择apache-maven-3.5.2-bin ...
- windows网络模型之重叠IO(完成例程)的使用
#include <WINSOCK2.H> #include <stdio.h> #define PORT 5150 #define MSGSIZE 1024 #pragma ...
- 初学MillerRabin素数测试
前言 \(MillerRabin\)素数测试是一种很实用的素数判定方法. 它只针对单个数字进行判定,因而可以对较大的乃至于\(long\ long\)范围内的数进行判定,而且速度也很快,是个十分优秀的 ...
- 线段树扫描线总结(POJ 1389)
扫描线算是线段树的一个比较特殊的用法,虽然NOIP不一定会考,但是学学还是有用的,况且也不是很难理解. 以前学过一点,不是很透,今天算是搞懂了. 就以这道题为例吧:嘟嘟嘟 题目的意思是在一个二维坐标系 ...
- P2382 化学分子式
luogu的oier化学一定都很好 这个题是让我们模拟计算化学方程式的过程. 和时间复杂度类似的题目. 我们可以根据括号,将求解分成若干个步骤. 从外部看,需要将一对括号看做一个整体.然后进行计算. ...
- HDU 1258 Sum It Up(dfs 巧妙去重)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...
- mui 的多图片上传
pickHead(){ var _this = this; plus.gallery.pick(function(path){ _this.headImage=path; var files = [{ ...