当前位置:网站首页>leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
2022-08-11 03:42:00 【小机double】
leetCode 110 平衡二叉树判断
题目描述
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点的左右两个子树的高度差的绝对值不超过1。
示例
给定二叉树 [3,9,20,null,null,15,7]
3
/ \
9 20
/ \
15 7
返回 true
给定二叉树 [1,2,2,3,3,null,null,4,4]
1
/ \
2 2
/ \
3 3
/ \
4 4
题目解析
与平衡二叉树等基础相关的可以看《数据结构之二叉树和平衡二叉树的实现》,根据题目的意思,我们需要对每个节点判断其左右子树的高度,并保证其高度差不超过1,即每个节点的平衡因子不超过1,如果查过1的话就不是平衡二叉树了,此时就要进行平衡化操作,不过这并不属于本题要讲述的内容。
其实只要使用递归的方式,并统计左右子树的高度来比较即可,代码如下:
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
if (!isBalanced(root.left) || !isBalanced(root.right)) {
//如果有左右子树不满足平衡二叉树条件则返回false
return false;
}
int leftDepth = maxDepth(root.left) + 1;
int rightDepth = maxDepth(root.right) + 1;
if (Math.abs(leftDepth - rightDepth) > 1) {
return false;
}
return true;
}
public int maxDepth(TreeNode root) {
//计算每颗树的最高深度
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
有代码可以看出,这其实是从底向上来判断一整颗树是否是平衡二叉树。
程序运行结果:

边栏推荐
- 大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
- Enter the starting position, the ending position intercepts the linked list
- Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
- DNS分离解析和智能解析
- 【FPGA】名词缩写
- A simple JVM tuning, learn to write it on your resume
- 图解LeetCode——640. 求解方程(难度:中等)
- 【ADI低功耗2k代码】基于ADuCM4050的ADXL363、TMP75的加速度、温度检测及串口打印、蜂鸣器播放音乐(孤勇者)
- KingbaseES有什么办法,默认不读取sys_catalog下的系统视图?
- typedef defines the structure array type
猜你喜欢

DNS separation resolution and intelligent resolution

es-head插件插入查询以及条件查询(五)

Echart地图的省级,以及所有地市级下载与使用

Is Redis old?Performance comparison between Redis and Dragonfly
![Binary tree related code questions [more complete] C language](/img/85/a109eed69cd54be3c8290e8dd67b7c.png)
Binary tree related code questions [more complete] C language

Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use

UNI-APP_iphone bottom safe area

【愚公系列】2022年08月 Go教学课程 036-类型断言

When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?

【FPGA】day22-SPI协议回环
随机推荐
Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
What are port 80 and port 443?What's the difference?
Qnet Weak Network Test Tool Operation Guide
电力机柜数据监测RTU
App基本框架搭建丨日志管理 - KLog
Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
阿里低代码框架 lowcode-engine 之自定义物料篇
获取链表长度
互换性与测量技术——表面粗糙度选取和标注方法
Ten Advanced Concepts of SQL Development
Traversal of DOM tree-----modify styles, select elements, create and delete nodes
图解LeetCode——640. 求解方程(难度:中等)
Day20 FPGA 】 【 - block the I2C read and write EEPROM
Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
oracle的基数会影响到查询速度吗?
【FPGA】day20-I2C读写EEPROM
移动端地图开发选择哪家?
怎么删除语句审计日志?
Build Zabbix Kubernetes cluster monitoring platform
Leetcode 669. 修剪二叉搜索树