当前位置:网站首页>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;
}
边栏推荐
- QT source code analysis -- QObject (2)
- leetcode 1642. Furthest Building You Can Reach(能到达的最远的建筑)
- 为什么ping不通,而traceroute却可以通
- RuntimeError: Missing dependencies:XXX
- mysql写的代码数据 增删查改等等
- Video Fusion communication has become an inevitable trend of emergency command communication. How to realize it based on easyrtc?
- Easycvr invokes the interface parameter acquisition method and precautions of device video recording on the page
- Rescue system -- the application of read-write separation
- Liunx Mysql安装
- Win10 cloud, add Vietnamese
猜你喜欢
K8S部署高可用postgresql集群 —— 筑梦之路
Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)
Liunx Mysql安装
Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同
表单图片上传在Chorme中无法查看请求体的二进制图片信息
打印出来的对象是[object object],解决方法
中国芯片独角兽公司
liunx服务器 telnet 带用户名 端口登陆方法
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
随机推荐
lombok 使用
[force deduction 10 days SQL introduction] Day3
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
Easycvr invokes the interface parameter acquisition method and precautions of device video recording on the page
小黑ai4code代码baseline啃食1
Blue screen error UNMOUNTABLE boot volume of the solution
orb slam build bug: undefined reference to symbol ‘_ ZN5boost6system15system_ categoryEv‘
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
第七章 操作位和位串(三)
Several schemes of PHP code encryption
一文讲透,商业智能BI未来发展趋势如何
pymysql 向MySQL 插入数据无故报错
Video Fusion communication has become an inevitable trend of emergency command communication. How to realize it based on easyrtc?
Pymysql inserts data into MySQL and reports an error for no reason
Picture tools
Qt 中发送自定义事件
偶然间得到的framework工具类 自用
RuntimeError: Missing dependencies:XXX
leetcode 1268. Search Suggestions System(搜索推荐系统)
ZUCC_编译语言原理与编译_实验06 07 语法分析 LL 分析