http://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void print(node *node) {
if (!node) return;
print(node->left);
cout << node->data << " ";
print(node->right);
} bool balanced(node *root, int &height) {
if (!root) return true;
int l = ;
int r = ;
if (!balanced(root->left, l) || !balanced(root->right, r)) return false;
if (l - r >= || r - l >= ) return false;
height = max(l, r) + ;
return true;
} int main() {
struct node* root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
root->left->left->left = new node();
int height = ;
if (balanced(root, height)) cout << "yes" << endl;
else cout << "NO" << endl;
return ;
}

最新文章

  1. Android自定义控件之自定义ViewGroup实现标签云
  2. 关于rc.local
  3. SQL Server连接SQL Server、SQL Server连接ORACLE 链接服务器
  4. viewWithTag获取subview规则详解
  5. 【MYSQL】常用命令备忘录
  6. Shell学习笔记 - 环境变量配置文件
  7. Linker Special Section Types
  8. python爬虫数据抓取方法汇总
  9. 关于RtlInitUnicodeString感想
  10. WinDBG中加载SOS和CLR
  11. css2.1实现图片添加阴影效果
  12. System.Web.Mvc 3.0.0.1 和 3.0.0.0 有什么区别?被 Microsoft ASP.NET MVC 的一次安全更新害惨了!!!
  13. Spark SQL整体架构
  14. POJ3662或洛谷1948 Telephone Lines
  15. 艾伦AI研究院发布AllenNLP:基于PyTorch的NLP工具包
  16. 通过反射实现圆角ImageView
  17. javaScript的内置对象以及一些常用的方法
  18. IE下target获得焦点时存在虚线的问题
  19. 解决;R语言使用sqldf库是报错&quot;Failed to connect to database: Error: Access denied for user &#39;..&#39;@&#39;localhost&#39; (using password: NO) Error in !dbPreExists : invalid argument type&quot;
  20. 20145218张晓涵_Web基础

热门文章

  1. Session和Cookie之间存在的区别与联系
  2. python random 学习
  3. Python内置函数之super()
  4. Swing与javafx直接调用
  5. NYOJ 492 King (状态压缩)
  6. UserScan的处理流程分析
  7. 【Mac系统】之Mysql数据库遇到修改数字密码的问题(SQL语法错误:ERROR 1064 (42000),密码策略等问题:ERROR 1819 (HY000))
  8. JQ中find()与filter()的区别
  9. 【文献阅读】Self-Normalizing Neural Networks
  10. readonly const关键字