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. List容器——LinkedList及常用API,实现栈和队列

    LinkedList及常用API ①   LinkedList----链表 ②   LinkedList类扩展AbstractSequentialList并实现List接口 ③   LinkedLis ...

  2. 九度oj 题目1361:翻转单词顺序

    题目描述: JOBDU最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“stu ...

  3. 【Luogu】P3704数字表格(莫比乌斯反演+大胆暴力)

    题目链接 给你们讲个笑话:Konoset是个sb,他快速幂的时候把幂次取模了. 原式差不多就是这样吧$\prod\limits_{i=1}^{n}\prod\limits_{j=1}^{m}f[gcd ...

  4. 【Luogu】P3768简单的数学题(杜教筛)

    题目链接 emm标题全称应该叫“莫比乌斯反演求出可狄利克雷卷积的公式然后卷积之后搞杜教筛” 然后成功地困扰了我两天qwq 我们从最基本的题意开始,一步步往下推 首先题面给出的公式是$\sum\limi ...

  5. kb-07线段树-08--区间开根

    /* hdu-4027 题目:区间开根求和查询: 因为是开根,所以要更新的话就要更新到叶子节点.如果区间里全是1或是0的话就步用继续更新了,查询的时候正常查询: */ #include<iost ...

  6. 如何应对pm2.5

    细颗粒物又称细粒.细颗粒.PM2.5:指环境空气中空气动力学当量直径小于等于 2.5 微米的颗粒物,也称细颗粒物.能较长时间悬浮于空气中,其在空气中含量(浓度)越高,就代表空气污染越严重.可吸入颗粒物 ...

  7. comet realization with ajax&php

    1.prepare front-end code, meta content-type cannot be ignored! as to the xhr, status should be 3 < ...

  8. 【MFC】利用单一对话框内的分页技术实现向导功能(转)

    原文转自 http://blog.csdn.net/yongh701/article/details/43793151 所谓的向导功能,在win32的程序中常常见于安装程序或者程序内的设置向导,该向导 ...

  9. 标准C程序设计七---65

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  10. ASP.NET路由应用及IIS配置(非MVC)

    一.前后台代码: Global.cs: using System.Web.Routing; ... void Application_Start(object sender, EventArgs e) ...