当前位置:网站首页>判断一棵二叉树是否为平衡二叉树
判断一棵二叉树是否为平衡二叉树
2022-07-01 16:26:00 【明朗晨光】
1、题目
给定一棵二叉树,判断这棵二叉树是不是平衡二叉树。
2、思路
使用二叉树的递归套路解决这个问题。
如果以 X 节点为根的一棵树是平衡二叉树,那么必须满足三个条件:
- 左子树是平衡的
- 右子树是平衡的
- 左右子树的高度差的绝对值小于 2
那么 X 节点需要向左树获取的信息:①是否平衡;②高度
同理,X 节点需要向右树获取的信息:①是否平衡;②高度
这样,就能知道这棵树是否平衡。
所以,使用结构体 Info
保存这两个信息,每个节点一视同仁,都要进行这样的处理。
3、实现
/************************************************************************* > File Name: 034.判断二叉树是否为平衡二叉树.cpp > Author: Maureen > Mail: [email protected] > Created Time: 五 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) {
}
};
//方法一:递归
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;
}
//方法二: 二叉树递归套路求解
class Info {
public:
bool isBalanced; //是否平衡
int 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;
}
边栏推荐
- Go 语言源码级调试器 Delve
- 制造业数字化转型究竟是什么
- 巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
- Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
- Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
- SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
- 数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
- The supply of chips has turned to excess, and the daily output of Chinese chips has increased to 1billion, which will make it more difficult for foreign chips
- Is the programmer's career really short?
- AI高考志愿填报:大厂神仙打架,考生付费围观
猜你喜欢
Stegano in the world of attack and defense
数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
Activity的生命周期和启动模式详解
MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
Tutorial on principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)
【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和
免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
How to restore the system of Sony laptop
Leetcode 77 combination -- backtracking method
随机推荐
Advantages, values and risks of chain games compared with traditional games
The supply of chips has turned to excess, and the daily output of Chinese chips has increased to 1billion, which will make it more difficult for foreign chips
MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
Principle of SSM framework
德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
[kotlin] Introduction to higher-order functions
SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)
Preliminary study on golang crawler framework
Go 语言怎么优化重复的 if err != nil 样板代码?
[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting
软件工程导论——第六章——详细设计
Hi Fun Summer, play SQL planner with starrocks!
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
What are the differences between PHP and DW
Go 语言怎么使用对称加密?
虚拟串口模拟器和串口调试助手使用教程「建议收藏」