当前位置:网站首页>110. 平衡二叉树-递归法
110. 平衡二叉树-递归法
2022-06-24 07:07:00 【Mr Gao】
110. 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:true
示例 2:
输入:root = [1,2,2,3,3,null,null,4,4]
输出:false
示例 3:
输入:root = []
输出:true
解题代码如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
int f(struct TreeNode* root,int *r){
if(root&&(*r)==0){
int a=f(root->left,r)+1;
int b=f(root->right,r)+1;
if(abs(a-b)>=2){
*r=1;
}
if(a>b){
return a;
}
else{
return b;
}
}
else{
return 0;
}
}
bool isBalanced(struct TreeNode* root){
int *r=(int *)malloc(sizeof(int));
*r=0;
int a= f(root,r);
if(*r==1){
return false;
}
return true;
}
边栏推荐
- 關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL
- 关于ETL看这篇文章就够了,三分钟让你明白什么是ETL
- 单目双视三维坐标确定
- rpiplay实现树莓派AirPlay投屏器
- uniapp 热更新后台管理
- 一文讲透,商业智能BI未来发展趋势如何
- [explain the difference between operation and maintenance and network engineering]
- ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
- PHP代码加密的几种方案
- Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
猜你喜欢
![[team management] 25 tips for testing team performance management](/img/bd/0ef55630de43efcf5aa663f3099fce.jpg)
[team management] 25 tips for testing team performance management

rpiplay实现树莓派AirPlay投屏器

Liunx Mysql安装

Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)

MySQL 因字符集问题插入中文数据时提示代码 :1366

What is the future development trend of Business Intelligence BI

一文讲透,商业智能BI未来发展趋势如何

【团队管理】测试团队绩效管理的25点小建议

Base64编码详解及其变种(解决加号在URL变空格问题)

Redis cluster data skew
随机推荐
every()、map()、forEarch()方法。数组里面有对象的情况
Vscode install the remote -wsl plug-in to connect to the local WSL
Variable declaration and some special variables in shell
xargs使用技巧 —— 筑梦之路
数据中台:数据中台技术架构详解
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
PHP代码加密+扩展解密实战
提高INSERT速度
数据中台:数据中台全栈技术架构解析,附带行业解决方案
利用sonar做代码检查
剑指 Offer 55 - I. 二叉树的深度-dfs法
疫情、失业,2022,我们高喊着摆烂和躺平!
相机投影矩阵计算
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
Rescue system -- the application of read-write separation
WebRTC系列-网络传输之5选择最优connection切换
Using skills of xargs -- the way to build a dream
获取屏幕宽高工具类
Earthly 容器镜像构建工具 —— 筑梦之路
Why can ping fail while traceroute can