当前位置:网站首页>【LeetCode】12. Balanced Binary Tree·平衡二叉樹
【LeetCode】12. Balanced Binary Tree·平衡二叉樹
2022-07-21 01:05:00 【AQin1012】
題目描述
英文版描述
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.
英文版地址
https://leetcode.com/problems/balanced-binary-tree/
中文版描述
給定一個二叉樹,判斷它是否是高度平衡的二叉樹。 本題中,一棵高度平衡二叉樹定義為: 一個二叉樹每個節點 的左右兩個子樹的高度差的絕對值不超過 1 。
示例 1: 輸入:root = [3,9,20,null,null,15,7] 輸出:true
示例 2: 輸入:root = [1,2,2,3,3,null,null,4,4] 輸出:false
示例 3: 輸入:root = [] 輸出:true
提示:
樹中的節點數在範圍 [0, 5000] 內
-104 <= Node.val <= 104
中文版地址
https://leetcode.cn/problems/balanced-binary-tree/
解題思路
利用二叉搜索樹的一個重要特性特性,即的左右子樹也分別為二叉搜索樹
解題方法
俺這版
/**
* 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;
}
}
官方版
方法一:自頂向下的遞歸
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;
}
}
}
方法二:自底向上的遞歸
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;
}
}
}
總結
數據結構其實就是利用特殊的結構來增加操作(查找等)數據的效率o(^▽^)o
边栏推荐
- Glue terraform ecology to kubernetes world
- Unity Shader着色器学习(一)
- AVL tree
- CCTV news "Jinan rent quota invoice by hand" news channel_ People's network
- PX4使用P900数传
- Web APIs DOM event delegation + comprehensive case
- STM32移植LVGL8.2
- 【故事证明和概率公理】
- CCTV news "Shanghai rent quota invoice by hand" news channel_ People's network
- org.xml.sax.SAXParseException无法读取方案文档
猜你喜欢
【731. 我的日程安排表 II】
Leetcode- number of occurrences of numbers in the array (single dog problem)
Excellent disaster recovery solutions in 2022
Win11如何开启任务栏多样化?Win11开启新任务栏的方法
NVIDIA NX usage notes
Net question and answer: is there the most efficient way to check large files in C?
Web APIs DOM event delegation + comprehensive case
如何查看Win11可以升级22h2?Win11升级22h2的方法
LeetCode_78_子集
DNS域名解析
随机推荐
性能监控 之 Prometheus 三剑客安装案例
C # understand these 100 + lines of code, and you will really get started (Classic)
Unlock high scores | eBay deepens user experience and optimizes large screen device applications
Leetcode- number of occurrences of numbers in the array (single dog problem)
Unity中的Animation动画倒播、正播
翻译UE官方关于UObject基础的文档
实习打怪之路:npm和yarn的区别
我想在label标签上显示SQL数据库表里的记录数
QT_qss文件简易使用教程
Voulez - vous assurer la sécurité des logiciels à faible coût? Cinq missions de sécurité méritent d'être examinées
程序环境和预处理详解
xss漏洞的一些思考
开发测试平台难吗?
“OSError: [WinError 126] 找不到指定的模块”
网络安全技术的新趋势探讨
Some rules for implicit conversion of SAP ABAP character and string variables
Unity Shader着色器学习(二)
Dynamic memory management + flexible array
tkinter各种控件库控件创建速度比较
Modeling essay series 144 SCLC engineering experiment