Leetcode_110_Balanced Binary Tree
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42218839
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
思路:
(1)题意为判断一颗树是否为平衡二叉树。(PS:平衡二叉树的特性:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树)
(2)我发现这道题和之前的好多题求解方法类似,属于一解对多题的情形,可以参考文章从"按层次输出二叉树"到"求解二叉树深度"的总结。你只需要知道如何求解二叉树的深度就可很快得到答案。二叉树深度求解算法可以参照二叉树深度求解算法。
(3)一旦我们知道二叉树深度的求解算法后,就可以对二叉树递归判断左右两个子树的深度之差,如果深度差大于1或小于-1,则不是平衡二叉树;否则,继续遍历该节点下的子树,直到全部遍历完为止。
(4)希望本文对你有所帮助。
算法代码实现如下:
public static boolean isBalanced(TreeNode root) {
if (root == null)
return true;
int distance = getDeep(root.left) - getDeep(root.right);
if (distance > 1 || distance < -1)
return false;
else
return isBalanced(root.left) && isBalanced(root.right);
}
// 最大深度
public static int getDeep(TreeNode root) {
if (root == null)
return 0;
int level = 0;
LinkedList<TreeNode> list = new LinkedList<TreeNode>();
list.add(root);
int first = 0;
int last = 1;
while (first < list.size()) {
last = list.size();
while (first < last) {
if (list.get(first).left != null) {
list.add(list.get(first).left);
}
if (list.get(first).right != null) {
list.add(list.get(first).right);
}
first++;
}
level++;
}
return level;
}
Leetcode_110_Balanced Binary Tree的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- Go 语言循环语句
在不少实际问题中有许多具有规律性的重复操作,因此在程序中就需要重复执行某些语句. 以下为大多编程语言循环程序的流程图: Go 语言提供了以下几种类型循环处理语句: 循环类型 描述 for 循环 重复执 ...
- 《读书报告 -- Elasticsearch入门 》--简单使用(2)
<读书报告 – Elasticsearch入门 > ' 第四章 分布式文件存储 这章的主要内容是理解数据如何在分布式系统中存储. 4.1 路由文档到分片 创建一个新文档时,它是如何确定应该 ...
- Zookeeper命令行操作(常用命令;客户端连接;查看znode路径;创建节点;获取znode数据,查看节点内容,设置节点内容,删除节点;监听znode事件;telnet连接zookeeper)
8.1.常用命令 启动ZK服务 bin/zkServer.sh start 查看ZK服务状态 bin/zkServer.sh status 停止ZK服务 bin/zkServer.sh stop 重启 ...
- Mybatis 批量插入、批量更新
合理的使用批量插入.更新对性能优化有很大的作用,速度明显快了N倍. 要注意数据库连接串后面要新增:&allowMultiQueries=true,表示一个sql ...
- Bootstrap3 排版-对齐
通过文本对齐类,可以简单方便的将文字重新对齐. Left aligned text. Center aligned text. Right aligned text. Justified text. ...
- Spring的DataSource配置、将Hibernate配置全部写到Spring配置
DataSource可以集中管理数据库连接,减少维护工作量,使部署更简单: Spring的DataSource配置:(Spring数据源配置)这里使用dbcp,还有很多其他的如c3p0,jdbc,jn ...
- 给定整数a1、a2、a3、...、an,判断是否可以从中选出若干个数,使得它们的和等于k(k任意给定,且满足-10^8 <= k <= 10^8)。
给定整数a1.a2.a3.....an,判断是否可以从中选出若干个数,使得它们的和等于k(k任意给定,且满足-10^8 <= k <= 10^8). 分析:此题相对于本节"寻找满 ...
- 复习java逻辑---实现猜数字游戏
package shopping; import java.util.*; public class SendGiftMenu { public static void main(String[] a ...
- 18 UI美化自定义形状shape
自定义某个控件的形状 如 圆角 巨型 环形 : 在工程文件的新建 res/drawable/shape文件(以下键一个圆角) <?xml version="1.0" enco ...
- 05 Activity 现场保护
我们接电话然后返回的 Activity将会被毁了重新创建 所以我们需要在摧毁记录一些信息还原Activity 摧毁时: //在Activi被摧毁的调用此方法 @Override protected v ...