题目

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,只需要求得左右子树的高度,比较判定即可。

AC代码

class Solution {
public:
//判断二叉树是否平衡
bool isBalanced(TreeNode* root) {
if (!root)
return true; int lheight = height(root->left), rheight = height(root->right); if (abs(lheight - rheight) <= 1)
return isBalanced(root->left) && isBalanced(root->right);
else
return false;
}
//求树的高度
int height(TreeNode *root)
{
if (!root)
return 0;
else
return max(height(root->left), height(root->right)) + 1;
}
};

GitHub测试程序源码

最新文章

  1. Twitter面试题蓄水池蓄水量算法(原创 JS版,以后可能会补上C#的)
  2. 【写给大家看的CSS】定位元素:使用position/display布局
  3. SCRUM 流程的步骤2: Spring 计划
  4. Centos7下dnscrypt-proxy安装
  5. SQL 订阅发布备注
  6. rsync+inotify实现远程数据备份
  7. 【Beta版本】冲刺-Day1
  8. sum(iterable[, start]) 对集合求和
  9. html中子div用了浮动怎样让父div的大小自动撑开(清除浮动)
  10. canvas图表详解系列(5):雷达(面积)图
  11. Mysql 查询重复的记录
  12. python学习笔记(7)
  13. MongoDB安全使用指引
  14. Month format:number to English abbre
  15. P1028 数的计算
  16. ASP.NET CORE Swagger
  17. linux命令(及解压tar.gz文件)
  18. linux shell的for循环语法是怎样的?
  19. Python Signal 信号
  20. iOS原生项目集成React Native模块

热门文章

  1. [软件工程基础]PhyLab 技术规格说明书
  2. Asp.net开发必备51种代码
  3. AJPFX关于子类父类中的构造
  4. 洛谷 P1048 采药
  5. hihocoder1776 序列
  6. Azure Powershell blob中指定的vhd创建虚拟机
  7. Fragment(一)--Fragment用法常见问题
  8. windows命令行快速启动软件
  9. Windows上SVN服务器搭建【转】
  10. 利用基于@AspectJ的AOP实现权限控制