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的更多相关文章

  1. 递归 - Leetcode 110 判断二叉树是否为平衡二叉树

    110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  2. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  3. 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 + ...

  4. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  5. 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 ...

  6. 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 ...

  7. Java实现 LeetCode 110 平衡二叉树

    110. 平衡二叉树 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9 ...

  8. LeetCode 110. Balanced Binary Tree(判断平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

随机推荐

  1. CyclicBarrier 使用说明

    字面意思回环栅栏,通过它可以实现让一组线程等待至某个状态之后再全部同时执行.叫做回环是因为当所有等待线程都被释放以后,CyclicBarrier可以被重用.   主要方法:      public i ...

  2. 【shell】 I/O重定向

    1.基本概念  a.I/O重定向通常与 FD有关,shell的FD通常为10个,即 0-9:  b.常用FD有3个,为0(stdin,标准输入).1(stdout,标准输出).2(stderr,标准错 ...

  3. Python 基础【第五篇】元组和列表

    一 .Python之列表: 其实所谓的列表我个人感觉和shell 中的数组是一样的(只是个人见解哦),列表其实说白了就是元素的组合: 格式: Name = [a,b,c,d] 下标: 每一个列表中的元 ...

  4. javascript开发中的封装模式(转)

    var bgAuido={ audio : pingfan.$$('audio'), audioBtn : pingfan.$$('audioBtn'), init : function(){ var ...

  5. INSTALLING QUARTUS II V.13.1 64 BIT ON RHEL/CENTOS 6 64 BIT

    http://www.digitalsolutionslab.com/installing-quartus-ii-v-13-1-64-bit-on-rhelcentos-6-64-bit/ I hav ...

  6. golang 依赖控制反转(IoC)

    主流开发语言,为了达到项目间的低耦合,都会借助IoC框架来实现.即抽象和实现分离,使用抽象层,不用关心这些抽象层的具体实现:抽象层的实现,可以独立实现.现在比较流行的领域驱动设计(ddd),为了达到将 ...

  7. [转]分布式系统为什么需要 Tracing?

    分布式系统为什么需要 Tracing?   先介绍一个概念:分布式跟踪,或分布式追踪.   电商平台由数以百计的分布式服务构成,每一个请求路由过来后,会经过多个业务系统并留下足迹,并产生对各种Cach ...

  8. mvn常见命令

    http://www.cnblogs.com/adolfmc/archive/2012/07/31/2616908.html 创建一个简单的Java工程:mvn archetype:create -D ...

  9. c# json转Dictionary字典

     JavaScriptSerializer s = new JavaScriptSerializer();  string jsonTexts = "{\"count\" ...

  10. Android 6.0doze和standby 的一点理解

    之前写的压力测试程序,在开发版和5.0上面测试好好的,即使熄灭屏幕也会跑.我的程序主要是在Activity里面开启了一个thread,其中不断在界面上显示任务信息.换到6.0的 机器上面,发现一个现象 ...