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.

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode *root) {
if(root==NULL) return true;
int l=height(root->left);
int r=height(root->right);
if(l<||r<||l-r>||r-l>) return false;
return true;
}
int height(TreeNode *root){
if(root==NULL) return ;
int l=height(root->left);
int r=height(root->right);
if(l<||r<||l-r>||r-l>) return -;
return max(l,r)+;
}
};

其他做法:

转自:http://blog.csdn.net/feliciafay/article/details/18348065

 // 68ms过大集合  简洁版
public:
int cntHeight(TreeNode *root) {
if(root == NULL) return ;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < || r < || abs(l-r) > ) return -; //自定义 return -1,表示不平衡的情况
else return max(l, r) + ;
}
bool isBalanced(TreeNode *root) {
if(root == NULL) return true;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < || r < || abs(l-r) > ) return false;
else return true;
}
 // 68ms过大集合  更简洁版
public:
int cntHeight(TreeNode *root) {
if(root == NULL) return ;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < || r < || abs(l-r) > ) return -; //自定义 return -1,表示不平衡的情况
else return max(l, r) + ;
}
bool isBalanced(TreeNode *root) {
if(root == NULL) return true;
int l = cntHeight(root->left);
int r = cntHeight(root->right);
if(l < || r < || abs(l-r) > ) return false;
else return true;
}

Balanced Binary Tree——数是否是平衡,即任意节点左右字数高度差不超过1的更多相关文章

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

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

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

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

  3. 110.Balanced Binary Tree Leetcode解题笔记

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

  4. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  5. 【leetcode】Balanced Binary Tree(middle)

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

  6. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

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

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

  8. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. python--命名规范及常见的数据类型

    1.python的命名规范 (1)不能以数字开头,不能出现中文. (2)命名以字母开头,包含数字,字母(区分大小写),下划线. (3)不能包含关键字,见名知意. 2.python常见的数据类型 (1) ...

  2. Jmeter生成8位不重复的随机数

    jmeter的time函数${__time(,)}  :  默认该公式精确到毫秒级别, 13位数 ${__time(/1000,)}  : 该公式精确到秒级别, 10位数 ${__time(yyyy- ...

  3. python基础补漏-09-反射

    isinstance class A: passclass B(A): pass b = B()print isinatance(b,A)issubclass 判断某一个类是不是另外一个类的派生类 # ...

  4. Python学习-day7 类 部分socket

    这周还是继续关于类的学习,在面向对象的学习过程中又学习了网络编程,并且提交了编写FTP的作业. 复习一下类的相关概念和定义 类      属性           实例变量:内存中           ...

  5. STL之set容器的总结

    最近做了很多题型,都是用简单的STL就解决了,深刻的感觉到STL的伟大力量,但是本人在遇到问题的时候还是喜欢用常规的算法去解决问题,脑袋笨没办法,有时候根本想不到用STL去解决一些问题 往往都是砍了网 ...

  6. [Err] 1022 - Can't write; duplicate key in table '#sql-1500_26'

        今天用powerdesigner修改了一些外键关系,有两个外键的名字取一样的,忘记改了.然后在用navicat运行sql文件时,报出[Err] 1022 - Can't write; dupl ...

  7. JDBC 学习笔记(三)—— JDBC 常用接口和类,JDBC 编程步骤

    1. JDBC 常用接口和类 DriverManager 负责管理 JDBC 驱动的服务类,程序中主要的功能是获取连接数据库的 Connection 对象. Connection 代表一个数据库连接对 ...

  8. ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)

    1057: Beautiful Garden Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 25  Solved: 12 [Submit][Statu ...

  9. SpringBoot使用Junit4单元测试

    SpringBoot2.0笔记 本篇介绍Springboot单元测试的一些基本操作,有人说一个合格的程序员必须熟练使用单元测试,接下来我们一起在Springboot项目中整合Junit4单元测试. 本 ...

  10. System and method for cache management

    Aspects of the invention relate to improvements to the Least Recently Used (LRU) cache replacement m ...