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 ...
随机推荐
- Hadoop加速器GridGain
GridGain的Hadoop加速器 像GridGain等内存网格产品(IMDG)不仅可以作为简单的缓存,加速Hadoop中MapReduce计算也是IMDG的一个亮点.这样内存计算领域又多了一种思路 ...
- TensorFlow入门和示例分析
本文以TensorFlow源码中自带的手写数字识别Example为例,引出TensorFlow中的几个主要概念.并结合Example源码一步步分析该模型的实现过程. 一.什么是TensorFlow 在 ...
- hiredis的各种windows版本
hiredis的各种windows版本(金庆的专栏 2016.12)hiredis 是内存数据库 redis 的客户端C库, 不支持Windows.hiredis的Windows移植版本有许多:des ...
- Redis之(二)数据类型及存储结构
Redis支持五中数据类型:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sortedset:有序集合). Redis定义了丰富的原语命令,可以直接与Redis ...
- Dynamics CRM2016 Update or Create parentcustomerid in Contact using web api
联系人实体中有个特殊的字段parentcustomerid 在通过web api创建或更新记录时,如果在给这个字段赋值时当做查找字段对待的话,那你就会遇到问题了,报错信息如下 正确的赋值方式如下
- java 随机数高效生成
分享牛,分享牛原创.近期去面试经常被问到java如何生产随机数,以及生成很大的字符串保证不能重复,还要考虑性能,之前本人面试别人的时候,可能不会问这个问题.既然这个java随机数问题经常被问到,那咱们 ...
- Detailed Item Cost Report (XML) timed out waiting for the Output Post-processor to finish
In this Document Symptoms Cause Solution References APPLIES TO: Oracle Cost Management - Ver ...
- Android 5.0新控件——TextInputLayout
Android 5.0(M)新控件--TextInputLayout 介绍之前,先直观的看一下效果 TextInputLayout其实是一个容器,他继承自LinearLayout,该容器是作用于Tex ...
- SQLite 语法(http://www.w3cschool.cc/sqlite/sqlite-syntax.html)
SQLite 语法 SQLite 是遵循一套独特的称为语法的规则和准则.本教程列出了所有基本的 SQLite 语法,向您提供了一个 SQLite 快速入门. 大小写敏感性 有个重要的点值得注意,SQL ...
- Struts 1 之<html>标签库
<html:html>标签 <html:html>标签用于在网页开头生成HTML的<html>元素,它只有一个用于显示用户语言的lang属性: <html:h ...