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

边栏推荐
猜你喜欢

leetcode刷题第13天二叉树系列之《98 BST及其验证》

Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
![[FPGA] day19- binary to decimal (BCD code)](/img/d8/6d223e5e81786335a143f135385b08.png)
[FPGA] day19- binary to decimal (BCD code)

Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method

Environment configuration of ESP32 (arduino arduino2.0 VScode platform which is easy to use?)

2022-08-10 第六小组 瞒春 学习笔记

Qnet Weak Network Test Tool Operation Guide

大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?

互换性与测量技术——表面粗糙度选取和标注方法

【FPGA】设计思路——I2C协议
随机推荐
移动端地图开发选择哪家?
Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
LeetCode刷题第16天之《239滑动窗口最大值》
FTP错误代码列表
UNI-APP_iphone bottom safe area
I didn't expect MySQL to ask these...
Description of ESB product development steps under cloud platform
Leetcode 450. 删除二叉搜索树中的节点
MYSQLg advanced ------ clustered and non-clustered indexes
Kubernetes集群搭建Zabbix监控平台
【FPGA】名词缩写
多串口RS485工业网关BL110
es-head插件插入查询以及条件查询(五)
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?
机器学习怎么学?机器学习流程
MYSQLg advanced ------ return table
uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
The development of the massage chair control panel makes the massage chair simple and intelligent
【FPGA】day22-SPI协议回环