判定一棵二叉树是不是二叉平衡树。

链接:https://oj.leetcode.com/problems/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比较,递归进行这个操作就可以完成判定。

回顾一下二叉树的概念,平衡二叉树基于二叉查找树(Binary Search Tree)
二叉查找树或者是一棵空树;
或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
二叉查找树是插入,排序,查找的平均时间复杂度都是O(Log(n)),最差情况是O(n),
二叉树的平衡因子是该结点的左子树的深度减去它的右子树的深度,平衡二叉树的BF因子绝对为1。

下面用Java完成Solution,注意空树的情况:

public class BalancedBinaryTreeSolution {
/**
* 访问局部内部类必须先有外部类对象,此处必须用Static修饰
*/
// public static class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
// TreeNode(int x){ val = x;}
// } //OJ支持JDK的Math函数
public boolean isBalanced(TreeNode root) {
//注意空树的情况
if(root == null)
return true; int leftDepth=getDepth(root.left);
int rightDepth=getDepth(root.right);
int altitude=rightDepth-leftDepth;
//注意只有一个顶点的情况
if(Math.abs(altitude)>1)
return false;
else
//递归比较
return isBalanced(root.left) && isBalanced(root.right);
} private int getDepth(TreeNode node){
if(node == null)
return 0;
//递归求深度
return 1+Math.max(getDepth(node.left), getDepth(node.right));
}
}

LeetCode之Balanced Binary Tree 平衡二叉树的更多相关文章

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

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

  2. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  3. LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)

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

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

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

  5. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

    4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...

  6. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  7. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  8. 【leetcode】Balanced Binary Tree(middle)

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

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

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

随机推荐

  1. 项目总结—jQuery EasyUI- DataGrid使用

    http://blog.csdn.net/zwk626542417/article/details/18839349 概要 jQuery EasyUI是一个基于jquery的集成了各种用户界面的框架, ...

  2. visual studio 2012如何彻底删除TFS上的团队项目

    http://www.cnblogs.com/zfanlong1314/p/3378441.html 本人的TFS地址:https://zfanlong1314.visualstudio.com/ 最 ...

  3. iBatisnet系列(二) 配置运行环境和日志处理

    http://hjf1223.cnblogs.com/archive/2006/04/24/383119.aspx 刚爬完鼓山回来,想到这篇刚刚开始,不敢怠慢,洗完澡休息一下就到电脑旁边来了.现在我开 ...

  4. shell与变量的声明的操作

    1.给命令起别名:alias 执行下面命令后,可以使用dir代替ls –l 命令,显示目录中的文件详细信息: 还可以用一个别名表示几个命令 的结合: 2.ps:显示当前登录会话的所有活动进程: 3.更 ...

  5. Oracle存储过程中传入参数,传出字符串

    create or replace procedure getStr(usercode in varchar2,str out varchar2) is v_sql ); isEdit ); isAd ...

  6. hdu 1003 Max Sum(动态规划)

    解题思路: 本题在给定的集合中找到最大的子集合[子集合:集合的元素的总和,是所有子集合中的最大解.] 结果输出: 最大的子集合的所有元素的和,子集合在集合中的范围区间. 依次对元素相加,存到一个 su ...

  7. php排序 sort、rsort、asort、arsort、ksort、krsort

    sort() 函数用于对数组单元从低到高进行排序. rsort() 函数用于对数组单元从高到低进行排序. asort() 函数用于对数组单元从低到高进行排序并保持索引关系. arsort() 函数用于 ...

  8. 一个简单的web服务器

    写在前面 新的一年了,新的开始,打算重新看一遍asp.net本质论这本书,再重新认识一下,查漏补缺,认认真真的过一遍. 一个简单的web服务器 首先需要引入命名空间: System.Net,关于网络编 ...

  9. mysql 的设置

    网上的一些文章都已经比较老了,现在版本高了之后,其实配置是很省力的(不考虑什么负载的话) 分享全过程,出了文中提到的安装epel rpmfushion 源指令不同外,其他的过程也适用与Centos 5 ...

  10. matlab 更改横坐标坐标值的方向

    [转载]http://blog.sina.com.cn/s/blog_7270407901012dyd.html#post 非常有用的Matlab代码,收藏了.   function th=rotat ...