当前位置:网站首页>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】387. 字符串中的第一个唯一字符

input的聚焦后的边框问题

Telnet port login method with user name for liunx server

【LeetCode】541. 反转字符串 II

【使用 PicGo+腾讯云对象存储COS 作为图床】

The form image uploaded in chorme cannot view the binary image information of the request body

MySQL | 视图《康师傅MySQL从入门到高级》笔记

MBA-day25 最值问题-应用题

À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL

Jenkins自动化部署,连接不到所依赖的服务【已解决】
随机推荐
China chip Unicorn Corporation
[10 day SQL introduction] Day2
How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
leetcode 1642. Furthest building you can reach
所说的Get post:请求的区别,你真的知道了吗??????
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
liunx 更改 vsftpd 的端口号
数据中台:国内大厂中台建设架构集锦
Liunx change the port number of vsftpd
Matlab camera calibrator camera calibration
JS to find and update the specified value in the object through the key
一文详解|增长那些事儿
【使用 PicGo+腾讯云对象存储COS 作为图床】
剑指 Offer 55 - I. 二叉树的深度-dfs法
基于QingCloud的地理信息企业研发云解决方案
常用表情符号
mysql写的代码数据 增删查改等等
Distributed | how to make "secret calls" with dble