当前位置:网站首页>"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.
程序运行结果:

边栏推荐
- 【FPGA】day21- moving average filter
- Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
- js 将字符串作为js执行代码使用
- oracle的基数会影响到查询速度吗?
- VIT 源码详解
- Interchangeable Measurement Techniques - Geometric Errors
- Description of ESB product development steps under cloud platform
- Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
- Day20 FPGA 】 【 - block the I2C read and write EEPROM
- LeetCode刷题第10天字符串系列之《125回文串验证》
猜你喜欢

图解LeetCode——640. 求解方程(难度:中等)

Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method

Kubernetes集群搭建Zabbix监控平台

二叉树相关代码题【较全】C语言

Is Redis old?Performance comparison between Redis and Dragonfly

LeetCode刷题第17天之《3 无重复字符的最长子串》

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?

The development of the massage chair control panel makes the massage chair simple and intelligent

Description of ESB product development steps under cloud platform

Basic understanding of MongoDB (2)
随机推荐
The development of the massage chair control panel makes the massage chair simple and intelligent
高校就业管理系统设计与实现
2022-08-10 The sixth group Hiding spring study notes
LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
LeetCode刷题第10天字符串系列之《125回文串验证》
QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
论文精度 —— 2017 CVPR《High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis》
App基本框架搭建丨日志管理 - KLog
程序化交易与主观交易对盈利曲线的影响!
【FPGA】SDRAM
我的 archinstall 使用手册
二叉树相关代码题【较全】C语言
leetcode刷题第13天二叉树系列之《98 BST及其验证》
Detailed explanation of VIT source code
移动端地图开发选择哪家?
[FPGA] Design Ideas - I2C Protocol
【FPGA】名词缩写
【C语言】入门
【FPGA】abbreviation
VIT 源码详解