leetcode-110:判断平衡二叉树 Java
Balanced Binary Tree
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:
1.计算每个节点的高度
2.从根节点开始从上往下遍历,判断每个节点的左右子树是否是平衡的
缺点:每次遍历都要重新计算高度,很多节点的高度都重复计算了,时间复杂度o(n^2)
解法2:
从根节点开始,从上往下遍历,按照中序遍历的思想,从左右子节点向根节点遍历,一依次判断平衡状态,这样根结点可以重复利用已经计算的子节点的高度,只需要依次遍历整棵树。在遇到某个子树非平衡时,能直接结束,返回false。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.math.*;
public class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null)
return true;
int leftH = getHeight(root.left);
int rightH = getHeight(root.right);
int diff = leftH - rightH;
if(diff>1 || diff<-1){
return false;
}else{
return isBalanced(root.left) && isBalanced(root.right);
} } int getHeight(TreeNode root){
if(root == null){
return 0;
}
return 1+Math.max(getHeight(root.left),getHeight(root.right));
} /**
public boolean isBalanced(TreeNode root) {
if(root == null)
return true; if(getHeight(root) == -1){
return false;
}else{
return true;
}
} public int getHeight(TreeNode root){
if(root == null)
return 0; int leftH = getHeight(root.left);
if(leftH == -1)
return -1; int rightH = getHeight(root.right);
if(rightH == -1)
return -1; if(leftH-rightH > 1 || leftH-rightH < -1)
return -1; return 1+(leftH>rightH?leftH:rightH); }
*/
}
leetcode-110:判断平衡二叉树 Java的更多相关文章
- 递归 - Leetcode 110 判断二叉树是否为平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- Java实现 LeetCode 110 平衡二叉树
110. 平衡二叉树 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9 ...
- LeetCode 110. Balanced Binary Tree(判断平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- Lucene 索引功能
Lucene 数据建模 基本概念 文档(doc): 文档是 Lucene 索引和搜索的原子单元,文档是一个包含多个域的容器. 域(field): 域包含“真正的”被搜索的内容,每一个域都有一个标识名称 ...
- Python 基础【第八篇】变量
1.变量定义: 给数据进行命名,数据的名字就叫做变量 2.变量格式: [变量名] = [值] 注:变量名命名需要满足下面两条准则 准则一:标示符开头不能为数字.不能包含空格.特殊字符准则二:标示符不能 ...
- PHP自动化测试
参考网址: 1. 基于Jenkins 实现php项目的自动化测试.自动打包和自动部署 2. PHP Jenkins,参考:http://jenkins-php.org/ 3.安装PHP_CodeSn ...
- Spark:用Scala和Java实现WordCount
http://www.cnblogs.com/byrhuangqiang/p/4017725.html 为了在IDEA中编写scala,今天安装配置学习了IDEA集成开发环境.IDEA确实很优秀,学会 ...
- 通过Workbook类 生成Excel导出数据
需求: 实现错误信息生成Excel保存到本地让用户查看. 刚开始使用了微软自带的Microsoft.Office.Interop.Excel类库. Microsoft.Office.Interop.E ...
- C#.net在后台执行javascript
string script = string.Format("<script type='text/javascript' >alert('{0}');</script&g ...
- Java_JVM学习笔记(深入理解Java虚拟机)___重点
http://chenzhou123520.iteye.com/category/196061 转载 JVM学习笔记(一):运行时数据区 JVM学习笔记(二):JVM中对象访问的两种方式 JVM学习笔 ...
- oracle中的exists 和not exists 用法 in与exists语句的效率问题
博文来源(oracle中的exists 和not exists 用法):http://chenshuai365-163-com.iteye.com/blog/1003247 博文来源( in与exi ...
- mysql net连接读取结果为乱码 Incorrect string value
在mysql直接查询中文正常,通过连接到mysql读取中文内容为乱码.同时插入中文内容也失败提示 Incorrect string value: '\xBC\xA4\xB7\xA2\xBF\xB4.. ...
- MySql事务及JDBC对事务的使用
一 .事务的几个重要特性 1. 原子性 事务内的每个内容不可分割,是一个统一的整体.或同时进行或同时消亡. 2.一致性 事务执行前和事务执行后,状态都是统一的.如A转B 100元,A和B数据总额度没有 ...