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.


题解:一A的题,写一个递归函数height计算树的高度,然后递归实现isBalance函数即可。代码如下:

 /**
* 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;
if(abs(height(root->left)-height(root->right)) > )
return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int height(TreeNode *root){
if(root == NULL)
return ;
int left = height(root->left);
int right = height(root->right);
return +(left>right?left:right);
}
};

【leetcode】Balanced Binary Tree的更多相关文章

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

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

  2. 【leetcode】Balanced Binary Tree(middle)

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

  3. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

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

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

  5. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  6. 【Leetcode】【Easy】Balanced Binary Tree

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

  7. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  8. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  9. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

随机推荐

  1. SQLServer中存储过程StoredProcedure创建及C#调用(转)

    此文作为入门了解用,转自http://www.2cto.com/database/201502/378260.html 存储过程就是已经编译好的.优化过的放在数据库服务器中的一些SQL语句:可供应用程 ...

  2. xml格式发送

    1. namespace xml格式发送 { /// <summary> /// 实体转Xml,Xml转实体类 /// </summary> /// <typeparam ...

  3. 如何从一个1G的文件中找到你所需要的东西

    如何从一个1G的文件中找到你所需要的东西,这个问题貌似面试的时候会经常问到.不过不论你用什么语言,肯定逃脱不了按指针读或者按块读. 这里介绍python的用法.本人亲自实验了,速度还可以. 如果你的文 ...

  4. sql语句偶记录

    查看表结构: show FULL COLUMNS FROM tablename;

  5. ctrip-apollo windows环境部署

    https://blog.csdn.net/u010286334/article/details/78389484转载过来的方法,尝试了没有成功,不晓得哪里错了,无法打包portal访问 1.下载源码 ...

  6. Arcgis:坐标系统极其转换

    1. ArcGIS中的坐标系统 ArcGIS中预定义了两套坐标系统,地理坐标系(Geographic coordinate system)和投影坐标系(Projectedcoordinate syst ...

  7. linux sublime python

    (三)配置python3编译环境 1.点击上部菜单栏Tools->Build System ->new Build System 2.点击之后,会出现一个空的配置文件,此时,往这个空配置文 ...

  8. jquery validate 详解

    jQuery校验官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一导入js库 <script src=&qu ...

  9. root 执行过程权限问题

    mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法 权限问题,授权 给 root  所有sql ...

  10. excel批量取消隐藏工作表

    按下"Alt+F11"键,在打开的"Microsoft Bisual Basic"窗口中,选择"插入——模块".,复制下面的代码,按F5键运 ...