当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

【PyTorch基础教程30】DSSM双塔模型代码解析

KaFormer个人笔记整理

【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理

MBA-day25 最值问题-应用题

pymysql 向MySQL 插入数据无故报错
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle

玄铁E906移植----番外0:玄铁C906仿真环境搭建
![[team management] 25 tips for testing team performance management](/img/bd/0ef55630de43efcf5aa663f3099fce.jpg)
[team management] 25 tips for testing team performance management

What is the future development trend of Business Intelligence BI

Prompt code when MySQL inserts Chinese data due to character set problems: 1366
随机推荐
opencv最大值滤波(不局限于图像)
数据中台:数据采集和抽取的技术栈详解
1528. 重新排列字符串
One article explains in detail | those things about growth
關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL
liunx服务器 telnet 带用户名 端口登陆方法
[10 day SQL introduction] Day2
Win11 blank when using VIM to view content in cmder
4275. Dijkstra序列
Determination of monocular and binocular 3D coordinates
随笔-反思
GradScaler MaxClipGradScaler
【NOI模拟赛】摆(线性代数,杜教筛)
What is graph neural network? Figure what is the use of neural networks?
Double pointer analog
【团队管理】测试团队绩效管理的25点小建议
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
JS to find and update the specified value in the object through the key
MySQL | 视图《康师傅MySQL从入门到高级》笔记
数据中台:数据中台技术架构详解