当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
【NOI模拟赛】寄(树形DP)
Matlab求解线性方程组Ax=b
Easynvr and easyrtc platforms use go language to manage projects. Summary of the use of govendor and gomod
Jenkins is deployed automatically and cannot connect to the dependent service [solved]
Jenkins自动化部署,连接不到所依赖的服务【已解决】
liunx 更改 vsftpd 的端口号
PHP代码加密的几种方案
Xiaohei ai4code code baseline nibble 1
Summary of methods in numpy
Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
第七章 操作位和位串(三)
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
Cloudbase database migration scheme
216. 组合总和 III-枚举法
RuntimeError: Missing dependencies:XXX
Shell basic operator -- arithmetic operator
Introduction to data platform
Shell pass parameters
Glusterfs replacement failure brick
Qt源码分析--QObject(2)









