当前位置:网站首页>"110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series
"110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series
2022-08-11 03:51:00 【small 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
题目解析
You can read about basics such as balanced binary trees《Implementation of binary tree and balanced binary tree data structure》,根据题目的意思,We need to determine the height of its left and right subtrees for each node,And ensure that its height difference does not exceed1,That is, the balance factor of each node does not exceed1,如果查过1It is not a balanced binary tree,At this point, the balancing operation is performed,However, this is not the content of this topic.
In fact, just use the recursive method,And count the height of the left and right subtrees to compare,代码如下:
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
if (!isBalanced(root.left) || !isBalanced(root.right)) {
//Returns if there are left and right subtrees that do not satisfy the balanced binary tree conditionfalse
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) {
//Calculate the maximum depth of each tree
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
有代码可以看出,This is actually to judge whether a whole tree is a balanced binary tree from the bottom up.
程序运行结果:

边栏推荐
- 按摩椅控制板的开发让按摩椅变得简约智能
- 互换性与测量技术-公差原则与选用方法
- AI + medical: for medical image recognition using neural network analysis
- I didn't expect MySQL to ask these...
- LeetCode刷题第17天之《3 无重复字符的最长子串》
- 没想到MySQL还会问这些...
- uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
- Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
- leetcode刷题第13天二叉树系列之《98 BST及其验证》
- 【FPGA】名词缩写
猜你喜欢

【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口

Qnet Weak Network Test Tool Operation Guide

Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)

没想到MySQL还会问这些...

EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?

【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion

The last update time of the tables queried by the two nodes of the rac standby database is inconsistent

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

The custom of the C language types -- -- -- -- -- - structure

【FPGA】day21-移动平均滤波器
随机推荐
Day20 FPGA 】 【 - block the I2C read and write EEPROM
机器学习中什么是集成学习?
Description of ESB product development steps under cloud platform
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)
Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
Echart地图的省级,以及所有地市级下载与使用
CTO说MySQL单表行数不要超过2000w,为啥?
What should I do if the channel ServerID is incorrect when EasyCVR is connected to a Hikvision Dahua device and selects another cluster server?
Kubernetes集群搭建Zabbix监控平台
学编程的第十三天
E-commerce project - mall time-limited seckill function system
Qnet弱网测试工具操作指南
Is there any way for kingbaseES to not read the system view under sys_catalog by default?
leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
移动端地图开发选择哪家?
Audio codec, using FAAC to implement AAC encoding
How to delete statements audit log?
MongoDB 基础了解(二)
VIT 源码详解