当前位置:网站首页>110. balanced binary tree recursive method
110. balanced binary tree recursive method
2022-06-24 08:52:00 【Mr Gao】
110. Balanced binary trees
Given a binary tree , Determine if it's a highly balanced binary tree .
In this question , A height balanced binary tree is defined as :
Every node of a binary tree The absolute value of the height difference between the left and right subtrees is not more than 1 .
Example 1:
Input :root = [3,9,20,null,null,15,7]
Output :true
Example 2:
Input :root = [1,2,2,3,3,null,null,4,4]
Output :false
Example 3:
Input :root = []
Output :true
The solution code is as follows :
/** * 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;
}
边栏推荐
- 数据中台:民生银行的数据中台实践方案
- leetcode 1642. Furthest building you can reach
- Telnet port login method with user name for liunx server
- Xtrabackup for data backup
- [pytorch basic tutorial 30] code analysis of DSSM twin tower model
- 110. 平衡二叉树-递归法
- 2022 spring recruitment interview summary
- Several schemes of PHP code encryption
- MySQL 因字符集问题插入中文数据时提示代码 :1366
- 表单图片上传在Chorme中无法查看请求体的二进制图片信息
猜你喜欢
pymysql 向MySQL 插入数据无故报错
[pytoch basic tutorial 31] youtubednn model analysis
MBA-day25 最值问题-应用题
【牛客】HJ1 字符串最后一个单词的长度
为什么ping不通,而traceroute却可以通
opencv最大值滤波(不局限于图像)
MySQL | 存储《康师傅MySQL从入门到高级》笔记
开源之夏中选名单已公示,基础软件领域成为今年的热门申请
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
Send custom events in QT
随机推荐
Xtrabackup for data backup
2020中国全国各省市,三级联动数据,数据机构(数据来自国家统计局官网)
MySQL 因字符集问题插入中文数据时提示代码 :1366
1528. 重新排列字符串
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
随笔-反思
玄铁E906移植----番外0:玄铁C906仿真环境搭建
Matlab camera calibrator camera calibration
数据中台:数据中台技术架构详解
Deep learning and neural networks: the six most noteworthy trends
Solution: Nan occurs in loss during model training
Using sonar for code checking
QT source code analysis -- QObject (2)
项目部署相关
What is the future development trend of Business Intelligence BI
疫情、失业,2022,我们高喊着摆烂和躺平!
Using ngrok for intranet penetration
DataX User Guide
Win11 blank when using VIM to view content in cmder
MBA-day25 最值问题-应用题