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.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
/ \
9 20
/ \
15 7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
/ \
2 2
/ \
3 3
/ \
4 4

Return false.

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int depth(TreeNode *root)
{
if (root == NULL) return ;
return max(depth(root->left), depth(root->right)) + ;
} bool isBalanced(TreeNode *root)
{
if (root == NULL) return true; int left = depth(root->left);
int right = depth(root->right); return abs(left - right) <= && isBalanced(root->left) && isBalanced(root->right); }
};

最新文章

  1. Theano3.3-练习之逻辑回归
  2. Yocto开发笔记之《工具使用:TFTP &amp; NFS &amp; SSH》(QQ交流群:519230208)
  3. 百度云+ KeePass 网络同步你的密码
  4. 【随笔】使用mOnOwall封禁某一个ip
  5. python数据结构-列表-基本操作
  6. javascript/jquery判断是否为undefined或是null!
  7. jquery动态移除/增加onclick属性详解
  8. 关于使用用友华表Cell控件按需打印行的方法
  9. php基础知识【函数】(7)url和ob函数
  10. HDU 3338 Kakuro Extension
  11. Clustering text documents using k-means
  12. cassandra 在window上的demo
  13. Springboot集成Thymeleaf
  14. 【小o地图Excel插件版】不止能做图表,还能抓58、大众点评网页数据...
  15. 7 种 join
  16. C#设计模式(7)——适配器模式(Adapter Pattern)(转)
  17. Apache Spark 2.2.0 新特性详细介绍
  18. SPRING的事务配置详解
  19. &lt;fmt:formatNumber&gt;标签
  20. [Algorithm] Print 2-D array in spiral order

热门文章

  1. Linux GCC下strstr的实现以及一个简单的Kmp算法的接口
  2. 为什么在 Java 中128==128返回false,而127==127返回true呢?
  3. wordpress防止垃圾邮件的另一种方法
  4. JavaScript项目总结一
  5. linux正则表达式与通配符练习
  6. uni验证码60秒倒计时
  7. 前端零基础入门:页面结构层HTML(3)
  8. 如何使用git把本地代码上传到远程仓库上
  9. http 缓存机制简介
  10. 反素数 Antiprime(信息学奥赛一本通 1625)(洛谷 1463)