当前位置:网站首页>判断一棵二叉树是否为平衡二叉树
判断一棵二叉树是否为平衡二叉树
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;
}
边栏推荐
- 游戏行业安全选择游戏盾,效果怎么样?
- 模板引擎Velocity 基礎
- 巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
- [nodemon] app crashed - waiting for file changes before starting... resolvent
- How to optimize repeated if err in go language= Nil template code?
- 免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
- 德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
- ssm框架原理
- Go language source level debugger delve
- Apple's self-developed baseband chip failed again, which shows Huawei Hisilicon's technological leadership
猜你喜欢

接口测试框架中的鉴权处理

StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!

数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)

Is the programmer's career really short?

阿里云、追一科技抢滩对话式AI

How to solve the problem that the battery icon of notebook computer does not display

Guide for high-end programmers to fish at work

Detailed explanation of activity life cycle and startup mode

Tutorial on principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)

Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
随机推荐
红队第8篇:盲猜包体对上传漏洞的艰难利用过程
Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
阿里云、追一科技抢滩对话式AI
Motion capture system for apple picking robot
Hi Fun Summer, play SQL planner with starrocks!
How to restore the system of Sony laptop
游戏行业安全选择游戏盾,效果怎么样?
sql刷题627. 变更性别
[SQL statement] Why do you select two Shanghai and query different counts here? I want it to become a Shanghai, and count only displays a sum
Template Engine Velocity Foundation
Go language source level debugger delve
P2592 [zjoi2008] birthday party (DP)
How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
Endeavouros mobile hard disk installation
Preliminary study on golang crawler framework
Apple's self-developed baseband chip failed again, which shows Huawei Hisilicon's technological leadership
Problèmes rencontrés dans le développement de la GI pour maintenir le rythme cardiaque en vie
巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
AI高考志愿填报:大厂神仙打架,考生付费围观
Stegano in the world of attack and defense