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. 关于RSS

    RSS(简易信息聚合)是一种消息来源格式规范,用以聚合经常发布更新数据的网站,例如博客文章.新闻.音频或视频的网摘.RSS文件(或称做摘要.网络摘要.或频更新,提供到频道)包含了全文或是节录的文字,再 ...

  2. -bash: lampp: command not found解决方案

    在/opt目录下安装完lampp后,需要到/opt/lampp/下执行lampp启动或者停止服务,如果在其余目录下执行lampp,会提示:-bash: lampp: command not found ...

  3. 【转】web测试方法总结

      一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直接输入特殊字符时 ...

  4. mysql同时向一个表中插入多条数据问题!!见详细

    INSERT INTO `表名` (`字段1`,`字段2`,`字段3`,`字段4`) values ('数组1数据1','数组1数据2','数组1数据3','数组1数据4'), ('数组2数据1',' ...

  5. 通过Workbook类 生成Excel导出数据

    需求: 实现错误信息生成Excel保存到本地让用户查看. 刚开始使用了微软自带的Microsoft.Office.Interop.Excel类库. Microsoft.Office.Interop.E ...

  6. UIButton 未响应原因分析

    1.父视图响应者链被阻断;例如:在 UILabel,UIImageView 控件添加UIButton,因为UILabel(userInteractionEnabled属性值为NO) 阻断了响应者链,所 ...

  7. UICollectionView的简单使用

    ChildModel.h #import <Foundation/Foundation.h> @interface ChildModel : NSObject @property (non ...

  8. OC2_数组操作

    // // main.m // OC2_数组操作 // // Created by zhangxueming on 15/6/11. // Copyright (c) 2015年 zhangxuemi ...

  9. JavaScript对象应用-字符串和图片对象

    1.1 应用 String对象截取特定文字   利用String 对象的charAt() 和 substring() 方法等,截取特定文字或字段文字显示在页面上 <html> <he ...

  10. jQuery 源码分析 8: 回头看jQuery的构造器(jQuery.fn,jQury.prototype,jQuery.fn.init.prototype的分析)

    在第一篇jQuery源码分析中,简单分析了jQuery对象的构造过程,里面提到了jQuery.fn.jQuery.prototype.jQuery.fn.init.prototype的关系. 从代码中 ...