问题:

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,但马上发现这是一个递归过程,每次递归返回的是深度,可是还得判断是否平衡,如果不平衡如何返回是否平衡,

然后你可能会想到使用两个函数,一个函数用于递归求深度,一个函数用于递归求是否平衡,如下:

public boolean isBalanced(TreeNode root) {
if(root==null) return true;
int l=depth(root.left);
int r=depth(root.right);
return ((int)Math.abs(l-r)<2)&&isBalanced(root.left) && isBalanced(root.right);
}
static int depth(TreeNode n){
if(n==null) return 0;
return Math.max(depth(n.left),depth(n.right))+1;
}

再然后你会发现时间复杂度为O(n^2),做了很多的无用功。

 

要想降低时间复杂度,就得想一个办法让我们在递归求深度的同时判断是否是平衡二叉树,也就是还是得解决求深度的时候递归返回值的问题,在LeetCode中discuss了一下,然后发现了大神们用了一个求深度时不可能出现的值轻松解决问题,关键代码如下:

public final int UNB = -99;
public int balanceJudge(TreeNode root){
if(root==null)return 0;
int l = balanceJudge(root.left);
int r = balanceJudge(root.right);
if(l==UNB || r== UNB || Math.abs(l-r)>1) return UNB;
return 1+(l>r?l:r);
}

最后只需要判定返回值否为UNB就可以知道改二叉树是否平衡了。。

 

 


完整代码如下(java):

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public final int UNB = -99;
public boolean isBalanced(TreeNode root) {
int result = balanceJudge(root);
if(result != UNB)return true;
else return false;
} public int balanceJudge(TreeNode root){
if(root==null)return 0;
int l = balanceJudge(root.left);
int r = balanceJudge(root.right);
if(l==UNB || r== UNB || Math.abs(l-r)>1) return UNB;
return 1+(l>r?l:r);
}
}

LeetCode——Balanced Binary Tree(判断是否平衡二叉树)的更多相关文章

  1. LeetCode Balanced Binary Tree (判断平衡树)

    题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...

  2. LeetCode: Balanced Binary Tree 解题报告

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

  3. [Leetcode] Balanced binary tree平衡二叉树

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

  4. [LeetCode] Balanced Binary Tree 平衡二叉树

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

  5. balanced binary tree(判断是否是平衡二叉树)

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

  6. Balanced Binary Tree 判断平衡二叉树

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

  7. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

  8. LeetCode - Balanced Binary Tree

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

  9. 【easy】110. Balanced Binary Tree判断二叉树是否平衡

    判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...

随机推荐

  1. BZOJ 2882: 工艺

    2882: 工艺 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 599  Solved: 268[Submit][Status][Discuss] D ...

  2. linux中ls命令

    ls跟dos下的dir命令是一样的都是用来列出目录下的文件 ls参数: -a: ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的, ...

  3. ng-switch

    <p>ng-switch : </p> <div ng-switch="isShow"><!--isShow是boolean值--> ...

  4. DataTable的过滤需要的数据

    DataView dv = datatable.DefaultView;           (1)      dv.RowFilter = "RowsId>3";  //此 ...

  5. LoadRunner 获取接口请求响应信息

    Action() { int nHttpRetCode; // 默认最大长度为256,get请求需注意缓存问题,需要根据content-length进行修改 web_set_max_html_para ...

  6. Schema

    Schema约束 1.namespace 相当于schema文件的id 2.targetNamespace属性 用来指定schema文件的namespace的值 3.xmlns属性 引入一个约束,它的 ...

  7. [NHibernate]视图处理

    目录 写在前面 文档与系列文章 视图 一个例子 总结 写在前面 前面的文章主要讲了对物理数据表的操作,当然了Nhibernate同样可以操作视图,本文将讲nhibernate对视图操作的种种. 文档与 ...

  8. R语言:用简单的文本处理方法优化我们的读书体验

    博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html 前言 延续之前的用R语言读琅琊榜小说,继续讲一下利用R语言做一些简单的文本处理.分词的事情.其实 ...

  9. Android高手速成--第二部分 工具库

    主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...

  10. Swift3.0P1 语法指南——方法

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...