当前位置:网站首页>"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.
程序运行结果:
边栏推荐
- 互换性与测量技术——表面粗糙度选取和标注方法
- Roewe imax8ev cube battery security, what blackening and swelling are hidden behind it?
- MYSQLg高级------回表
- Detailed explanation of VIT source code
- E-commerce project - mall time-limited seckill function system
- oracle的基数会影响到查询速度吗?
- Multi-serial port RS485 industrial gateway BL110
- 用户如何克服程序化交易中的情绪问题?
- 一次简单的 JVM 调优,学会拿去写到简历里
- 获取链表长度
猜你喜欢
Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
【FPGA】SDRAM
Multi-serial port RS485 industrial gateway BL110
Environment configuration of ESP32 (arduino arduino2.0 VScode platform which is easy to use?)
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
你不知道的 console.log 替代品
[FPGA] Design Ideas - I2C Protocol
机器学习中什么是集成学习?
【FPGA】abbreviation
【FPGA】SDRAM
随机推荐
MySQL数据库存储引擎以及数据库的创建、修改与删除
DNS分离解析和智能解析
How to rebuild after pathman_config and pathman_config_params are deleted?
Enter the starting position, the ending position intercepts the linked list
机器学习是什么?详解机器学习概念
LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
watch监听
The FTP error code list
Is there any way for kingbaseES to not read the system view under sys_catalog by default?
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
pathman_config、pathman_config_params 删除后,如何重建?
C语言之自定义类型------结构体
电商项目——商城限时秒杀功能系统
【FPGA】名词缩写
【FPGA】SDRAM
How can users overcome emotional issues in programmatic trading?
输入起始位置,终止位置截取链表
Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
STC8H development (15): GPIO drive Ci24R1 wireless module
En-us is an invalid culture error solution when Docker links sqlserver