当前位置:网站首页>#yyds干货盘点# 解决剑指offer: 判断是不是平衡二叉树
#yyds干货盘点# 解决剑指offer: 判断是不是平衡二叉树
2022-06-23 12:50:00 【51CTO】
1.简述:
描述
输入一棵节点数为 n 二叉树,判断该二叉树是否是平衡二叉树。在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树平衡二叉树(Balanced Binary Tree),具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
样例解释:
样例二叉树如图,为一颗平衡二叉树
注:我们约定空树是平衡二叉树。
数据范围:,树上节点的val值满足
要求:空间复杂度
,时间复杂度
输入描述:
输入一棵二叉树的根节点
返回值描述:
输出一个布尔类型的值
示例1
输入:
返回值:
示例2
输入:
返回值:
2.代码实现:
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null){
return true;
}
int left = deep(root.left);
int right = deep(root.right);
int v = Math.abs(left - right);
if(v > 1){
return false;
}
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
private int deep(TreeNode root) {
if(root == null){
return 0;
}
return Math.max(deep(root.left), deep(root.right)) + 1;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
边栏推荐
- In flinksql, the Kafka flow table and MySQL latitude flow table are left joined, and the association is made according to I'd. false
- 用户行为建模
- Excel-VBA 快速上手(一、宏、VBA、过程、类型与变量、函数)
- POW共识机制
- CDH mail alarm configuration
- Is there any discount for opening an account now? Is it safe to open a mobile account?
- Hanyuan hi tech 1-channel gigabit optical port to 4-channel Gigabit Ethernet electrical port Gigabit 1-optical 4-electric optical fiber transceiver
- 逆向调试入门-了解PE结构文件
- Esp32-c3 introductory tutorial problems ⑧ - blufi_ example. c:244: undefined reference to `esp_ ble_ gap_ start_ advertising
- Dataset之GermanCreditData:GermanCreditData数据集的简介、下载、使用方法之详细攻略
猜你喜欢

Qunhui 10 Gigabit network configuration and test

Basic data type and corresponding packing class

根据你的工作经历,说说软件测试中质量体系建设

MySQL使用ReplicationConnection導致的連接失效分析與解决

Hanyuan hi tech 1-way uncompressed 4k-dvi optical transceiver 4K HD uncompressed DVI to optical fiber 4k-dvi HD video optical transceiver

网络基础和框架

跟循泰国国内游宣传曲MV,像本地人一样游曼谷

判断测试结束的标准有哪些?

What should testers do if the requirements need to be changed when the project is half tested?

你管这破玩意儿叫 MQ?
随机推荐
能把SAP系统玩成鸡肋的公司,太有才了!
R语言使用MatchIt包进行倾向性匹配分析(设置匹配方法为nearest,匹配倾向性评分最近的对照组和病例组,1:1配比)、使用match.data函数构建匹配后的样本集合
Playing in Singapore in the hot summer: an inventory of indoor attractions and good places for night trips
< Sicily> 1001. Rails
CDH mail alarm configuration
A bug development means that the user will not operate like this, and there is no need to repair it. How should testers respond?
R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据(默认是升序排序)
在线文本过滤小于指定长度工具
js: 获取页面最大的zIndex(z-index)值
网络基础和框架
R语言将距离矩阵输入给hclust函数进行层次聚类分析,使用cutree函数进行层次聚类簇的划分、参数k指定聚类簇的个数、给每个样本都分配了簇标签
The GLM function of R language uses frequency data to build a binary logistic regression model. The input data for analysis is frequency data, which is transformed into normal sample data (split and s
1 channel 100m optical fiber transceiver 1 100m optical 1 100m electric desktop Ethernet optical fiber transceiver built-in power supply
Have you ever encountered incompatibility between flink1.15.0 and Flink CDC MySQL 2.2.1? f
2-optical-2-electric cascaded optical fiber transceiver Gigabit 2-optical-2-electric optical fiber transceiver Mini embedded industrial mine intrinsic safety optical fiber transceiver
The two 985 universities share the same president! School: true
测试时间不够怎么办?
Analyse et résolution des défaillances de connexion causées par MySQL utilisant replicationconnection
Excel-VBA 快速上手(一、宏、VBA、过程、类型与变量、函数)
What should I do if a serious bug occurs within the scope of my own test and I am about to go online?