当前位置:网站首页>[leetcode] 12. Arbre binaire équilibré · Arbre binaire équilibré
[leetcode] 12. Arbre binaire équilibré · Arbre binaire équilibré
2022-07-21 01:05:00 【Aqin1012】
Description du sujet
Description en anglais
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
Adresse en anglais
https://leetcode.com/problems/balanced-binary-tree/
Description de la version chinoise
Compte tenu d'un arbre binaire,Pour déterminer si c'est un arbre binaire très équilibré. Dans cette question,Un arbre binaire équilibré en hauteur est défini comme: Un arbre binaire par noeud La différence absolue de hauteur entre les sous - arbres gauche et droit de ne pas dépasser 1 .
Exemple 1: Entrée:root = [3,9,20,null,null,15,7] Produits:true
Exemple 2: Entrée:root = [1,2,2,3,3,null,null,4,4] Produits:false
Exemple 3: Entrée:root = [] Produits:true
Conseils:
Le nombre de noeuds dans l'arbre est dans la plage [0, 5000] Intérieur
-104 <= Node.val <= 104
Adresse de la version chinoise
https://leetcode.cn/problems/balanced-binary-tree/
Comment résoudre le problème
Une caractéristique importante de l'arbre de recherche binaire , Les sous - arbres gauche et droit sont également des arbres de recherche binaires
Comment résoudre le problème
Mon édition.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if (null == root) {
return true;
}
int step = getHeight(root.left) - getHeight(root.right);
return Math.abs(step) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
public int getHeight(TreeNode treeNode) {
if (null == treeNode) {
return 0;
}
return Math.max(getHeight(treeNode.left), getHeight(treeNode.right)) + 1;
}
}
Version officielle
Méthode 1:Récursion descendante
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
} else {
return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
}
public int height(TreeNode root) {
if (root == null) {
return 0;
} else {
return Math.max(height(root.left), height(root.right)) + 1;
}
}
}
Méthode 2:Récursion du bas vers le haut
class Solution {
public boolean isBalanced(TreeNode root) {
return height(root) >= 0;
}
public int height(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = height(root.left);
int rightHeight = height(root.right);
if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
return -1;
} else {
return Math.max(leftHeight, rightHeight) + 1;
}
}
}
Résumé
La structure des données est en fait l'utilisation d'une structure spéciale pour augmenter les opérations (Recherche, etc)Efficacité des donnéeso(^▽^)o
边栏推荐
- DeiT:注意力也能蒸馏
- NVIDIA NX usage notes
- CCTV news "Shenzhen rent quota invoice by hand" news channel_ People's network
- C asynchronous programming read this article is enough
- CCTV news "Hangzhou rent quota invoice by hand" news channel_ People's network
- 【JVM 系列】JVM 对象的分配策略
- 和利时LE5107_LE5106_自由口协议
- 【概率和计数】
- tkinter各种控件库控件创建速度比较
- 2022/7/19
猜你喜欢
随机推荐
Hollysys le5107_ LE5106_ Free port agreement
选择排序/插入排序/冒泡排序
“OSError: [WinError 126] 找不到指定的模块”
Read GeoTIFF grid file and assign values to points
Arduino reads the sensor data and saves it in Excel
ArrayList basic case
电气成套设备制造企业项目管理难点及解决方案
程序环境和预处理详解
LeetCode_78_子集
How to use parallel programming to improve task execution efficiency
Example of conventional deployment configuration of Huawei campus network
Want to ensure software security at low cost? Five safety tasks worth considering
"Oserror: [winerror 126] cannot find the specified module"
redisconnectionfactory could not autowired
【731. 我的日程安排表 II】
【Pygame小游戏】魂斗罗经典BOSS都回来了 准备好再次击败他们了吗?(附源码)
2022年优秀灾难恢复解决方案
构建乘积数组
VMware 启动报错:Exception 0xc0000005和windwos11没有Hyper-V的解决方法
哈希表(HashTable)