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
相关推荐
要加油GW8 分钟前
python使用vscode 需要配置全局的环境变量。
开发语言·vscode·python
B站计算机毕业设计之家12 分钟前
python图像识别系统 AI多功能图像识别检测系统(11种识别功能)银行卡、植物、动物、通用票据、营业执照、身份证、车牌号、驾驶证、行驶证、车型、Logo✅
大数据·开发语言·人工智能·python·图像识别·1024程序员节·识别
晨非辰19 分钟前
《数据结构风云》递归算法:二叉树遍历的精髓实现
c语言·数据结构·c++·人工智能·算法·leetcode·面试
Dream it possible!24 分钟前
LeetCode 面试经典 150_链表_LRU 缓存(66_146_C++_中等)(哈希表 + 双向链表)
c++·leetcode·链表·面试
快乐的钢镚子34 分钟前
思腾合力云服务器远程连接
运维·服务器·python
苏打水com35 分钟前
爬虫进阶实战:突破动态反爬,高效采集CSDN博客详情页数据
爬虫·python
夫唯不争,故无尤也1 小时前
三大AI部署框架对比:本地权重与多模型协作实战
人工智能·python·深度学习
be_humble1 小时前
GPU机器-显卡占用
pytorch·python·深度学习
阿郎_20114 小时前
python自动化脚本-简化留言
python·自动化
人邮异步社区4 小时前
推荐几本学习计算机语言的书
java·c语言·c++·python·学习·golang