当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
![Jenkins is deployed automatically and cannot connect to the dependent service [solved]](/img/fe/f294955a9bdf7492aab360e44e052d.png)
Jenkins is deployed automatically and cannot connect to the dependent service [solved]

A tip to read on Medium for free

Qt 中发送自定义事件

OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集

opencv最大值滤波(不局限于图像)

【NOI模拟赛】寄(树形DP)
![[team management] 25 tips for testing team performance management](/img/bd/0ef55630de43efcf5aa663f3099fce.jpg)
[team management] 25 tips for testing team performance management

It is enough to read this article about ETL. Three minutes will let you understand what ETL is
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL
随机推荐
Jenkins is deployed automatically and cannot connect to the dependent service [solved]
数据中台:数据采集和抽取的技术栈详解
Summary of methods in numpy
leetcode 1268. Search Suggestions System(搜索推荐系统)
数据库迁移从PostgreSQL迁移到 MYSQL
Earthly 容器镜像构建工具 —— 筑梦之路
1704. 判断字符串的两半是否相似
How to mount a USB hard disk with NTFS file format under RHEL5 system
leetcode 1642. Furthest Building You Can Reach(能到达的最远的建筑)
【生活思考】计划与自律
One development skill a day: how to establish P2P communication based on webrtc?
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
提高INSERT速度
数据中台:数据中台全栈技术架构解析,附带行业解决方案
Liunx Mysql安装
Shell basic operators -- relational operators
Easynvr and easyrtc platforms use go language to manage projects. Summary of the use of govendor and gomod
Qt 中发送自定义事件
String转Base64
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis