当前位置:网站首页>Judge whether a binary tree is a balanced binary tree
Judge whether a binary tree is a balanced binary tree
2022-07-01 16:47:00 【Bright morning light】
1、 subject
Given a binary tree , Judge whether this binary tree is a balanced binary tree .
2、 Ideas
Use the recursive routine of binary tree to solve this problem .
If the X A tree with nodes as roots is a balanced binary tree , Then three conditions must be met :
- The left subtree is balanced
- The right subtree is balanced
- The absolute value of the height difference between the left and right subtrees is less than 2
that X The information that the node needs to get from the left tree :① Is it balanced ;② Height
Empathy ,X The node needs the information obtained from the right tree :① Is it balanced ;② Height
such , We can know whether the tree is balanced .
therefore , Use structure Info
Save these two information , Every node is treated equally , Such treatment is required .
3、 Realization
/************************************************************************* > File Name: 034. Determine whether a binary tree is a balanced binary tree .cpp > Author: Maureen > Mail: [email protected] > Created Time: 5、 ... and 7/ 1 15:07:04 2022 ************************************************************************/
#include <iostream>
#include <ctime>
using namespace std;
class TreeNode {
public:
int value;
TreeNode *left;
TreeNode *right;
TreeNode(int data) : value(data) {
}
};
// Method 1 : recursive
int getHeight(TreeNode *root) {
if (root == nullptr) return 0;
return max(getHeight(root->left), getHeight(root->right)) + 1;
}
bool isBalanced1(TreeNode *root) {
if (root == nullptr) return true;
return isBalanced1(root->left)
&& isBalanced1(root->right)
&& abs(getHeight(root->left) - getHeight(root->right)) < 2;
}
// Method 2 : Binary tree recursive routine solution
class Info {
public:
bool isBalanced; // Is it balanced
int height; // Height
Info(bool isB, int h) : isBalanced(isB), height(h) {
}
};
Info *process(TreeNode *x) {
if (x == nullptr) {
return new Info(true, 0);
}
Info *leftInfo = process(x->left);
Info *rightInfo = process(x->right);
bool isBalanced = leftInfo->isBalanced && rightInfo->isBalanced && abs(leftInfo->height - rightInfo->height) < 2;
int height = max(leftInfo->height, rightInfo->height) + 1;
return new Info(isBalanced, height);
}
bool isBalanced2(TreeNode *root) {
return process(root)->isBalanced;
}
//for test
TreeNode *generate(int level, int maxLevel, int maxVal) {
if (level > maxLevel || (rand() % 100 /(double)101) < 0.5)
return nullptr;
TreeNode *root = new TreeNode(rand() % maxVal);
root->left = generate(level + 1, maxLevel, maxVal);
root->right = generate(level + 1, maxLevel, maxVal);
return root;
}
TreeNode *generateRandomBST(int maxLevel, int maxVal) {
return generate(1, maxLevel, maxVal);
}
int main() {
srand(time(0));
int maxLevel = 5;
int maxVal = 100;
int testTime = 1000001;
for (int i = 0; i < testTime; i++) {
TreeNode *root = generateRandomBST(maxLevel, maxVal);
//cout << "isBalanced1(root) = " << isBalanced1(root) << endl;
//cout << "isBalanced2(root) = " << isBalanced2(root) << endl;
if (isBalanced1(root) != isBalanced2(root)) {
cout << "Oops!" << endl;
break;
}
}
cout << "finish!" << endl;
return 0;
}
边栏推荐
- Detailed explanation of activity life cycle and startup mode
- Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
- AI高考志愿填报:大厂神仙打架,考生付费围观
- Rhcsa Road
- 挖财学堂班主任给的证券账户安全吗?能开户吗?
- 今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!
- SQL question brushing 586 Customers with the most orders
- Redis6.0 新功能
- 红队第10篇:coldfusion反序列化过waf改exp拿靶标的艰难过程
- China nylon 11 industry research and future forecast report (2022 Edition)
猜你喜欢
接口测试框架中的鉴权处理
C language input / output stream and file operation
你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
How to solve the problem that the battery icon of notebook computer does not display
Dataframe gets the number of words in the string
How to repair the laptop that cannot connect to the wireless network
MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
Template Engine Velocity Foundation
软件工程导论——第六章——详细设计
随机推荐
Rhcsa Road
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
单例模式的懒汉模式跟恶汉模式的区别
数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)
P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
软件工程导论——第六章——详细设计
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Redis6.0 新功能
Germany if was crowned with many awards. How strong is this pair of headphones? In depth evaluation of yinpo GTW 270 hybrid
复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
【PyG】文档总结以及项目经验(持续更新
游戏行业安全选择游戏盾,效果怎么样?
Principle of SSM framework
Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
Sword finger offer II 015 All modifiers in the string
数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
sql刷题1050. 合作过至少三次的演员和导演