Python | Leetcode Python题解之第110题平衡二叉树

题目:

题解:

python 复制代码
class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        def height(root: TreeNode) -> int:
            if not root:
                return 0
            leftHeight = height(root.left)
            rightHeight = height(root.right)
            if leftHeight == -1 or rightHeight == -1 or abs(leftHeight - rightHeight) > 1:
                return -1
            else:
                return max(leftHeight, rightHeight) + 1

        return height(root) >= 0
相关推荐
亓才孓9 分钟前
[leetcode]树的操作
算法·leetcode·职场和发展
搞不懂语言的程序员15 分钟前
装饰器模式详解
开发语言·python·装饰器模式
mosquito_lover119 分钟前
矿山边坡监测预警系统设计
人工智能·python·深度学习·神经网络·视觉检测
船长@Quant20 分钟前
PyTorch量化进阶教程:第二章 Transformer 理论详解
pytorch·python·深度学习·transformer·量化交易·sklearn·ta-lib
攻城狮7号1 小时前
【第一节】Python爬虫基础-HTTP基本原理
爬虫·python·python爬虫
IT乐手1 小时前
adb logcat 写文件乱码的解决方案
android·python
Python测试之道1 小时前
Deepseek API+Python 测试用例一键生成与导出 V1.0.6(加入分块策略,返回更完整可靠)
开发语言·python·测试用例
SRC_BLUE_171 小时前
Python GUI 编程 | QObject 控件基类详解 — 定时器
开发语言·数据库·python
loser~曹1 小时前
基于快速排序解决 leetcode hot215 查找数组中第k大的数字
数据结构·算法·leetcode
啊阿狸不会拉杆1 小时前
第二十一章:Python-Plotly库实现数据动态可视化
开发语言·python·plotly