Data Structure Binary Tree: How to determine if a binary tree is height-balanced?
2024-08-29 18:22:37
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 ;
}
最新文章
- Android自定义控件之自定义ViewGroup实现标签云
- 关于rc.local
- SQL Server连接SQL Server、SQL Server连接ORACLE 链接服务器
- viewWithTag获取subview规则详解
- 【MYSQL】常用命令备忘录
- Shell学习笔记 - 环境变量配置文件
- Linker Special Section Types
- python爬虫数据抓取方法汇总
- 关于RtlInitUnicodeString感想
- WinDBG中加载SOS和CLR
- css2.1实现图片添加阴影效果
- System.Web.Mvc 3.0.0.1 和 3.0.0.0 有什么区别?被 Microsoft ASP.NET MVC 的一次安全更新害惨了!!!
- Spark SQL整体架构
- POJ3662或洛谷1948 Telephone Lines
- 艾伦AI研究院发布AllenNLP:基于PyTorch的NLP工具包
- 通过反射实现圆角ImageView
- javaScript的内置对象以及一些常用的方法
- IE下target获得焦点时存在虚线的问题
- 解决;R语言使用sqldf库是报错";Failed to connect to database: Error: Access denied for user &#39;..&#39;@&#39;localhost&#39; (using password: NO) Error in !dbPreExists : invalid argument type";
- 20145218张晓涵_Web基础
热门文章
- Session和Cookie之间存在的区别与联系
- python random 学习
- Python内置函数之super()
- Swing与javafx直接调用
- NYOJ 492 King (状态压缩)
- UserScan的处理流程分析
- 【Mac系统】之Mysql数据库遇到修改数字密码的问题(SQL语法错误:ERROR 1064 (42000),密码策略等问题:ERROR 1819 (HY000))
- JQ中find()与filter()的区别
- 【文献阅读】Self-Normalizing Neural Networks
- readonly const关键字