当前位置:网站首页>"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.
程序运行结果:
边栏推荐
- Docker 链接sqlserver时出现en-us is an invalid culture错误解决方案
- A large horse carries 2 stone of grain, a middle horse carries 1 stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stone of grain. How to distribute it?
- CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
- 元素的BFC属性
- uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
- 使用jackson解析json数据详讲
- 【FPGA】day22-SPI协议回环
- What has programmatic trading changed?
- The development of the massage chair control panel makes the massage chair simple and intelligent
- 大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
猜你喜欢
移动端地图开发选择哪家?
LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
Redis老了吗?Redis与Dragonfly性能比较
Kubernetes集群搭建Zabbix监控平台
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
高校就业管理系统设计与实现
【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口
Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
STC8H开发(十五): GPIO驱动Ci24R1无线模块
随机推荐
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
LeetCode Hot Questions (12. The Best Time to Buy and Sell Stocks)
STC8H开发(十五): GPIO驱动Ci24R1无线模块
leetcode刷题第13天二叉树系列之《98 BST及其验证》
Get the length of the linked list
论文精度 —— 2017 CVPR《High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis》
Kubernetes集群搭建Zabbix监控平台
I didn't expect MySQL to ask these...
MYSQLg高级------聚簇索引和非聚簇索引
A large horse carries 2 stone of grain, a middle horse carries 1 stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stone of grain. How to distribute it?
DNS separation resolution and intelligent resolution
Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
图解LeetCode——640. 求解方程(难度:中等)
Homework 8.10 TFTP protocol download function
怎么删除语句审计日志?
学编程的第十三天
CTO说MySQL单表行数不要超过2000w,为啥?
App Basic Framework Construction丨Log Management - KLog