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
相关推荐
爱吃羊的老虎2 分钟前
Streamlit:快速创建应用界面,无需了解 Web 开发
前端·python
只想要搞钱9 分钟前
python 学习记录--1(开发工具,链接数据库mysql)
python·学习
星浩AI9 分钟前
深入理解 LlamaIndex:RAG 框架核心概念与实践
人工智能·后端·python
python开发笔记10 分钟前
can(6) canopen python库使用
服务器·网络·python
rgeshfgreh11 分钟前
Python变量与类型:从入门到精通
python
杨超越luckly11 分钟前
HTML应用指南:利用GET请求获取网易云热歌榜
前端·python·html·数据可视化·网易云热榜
灯下夜无眠12 分钟前
sklearn中fit、transform、fit_transform用法详解
人工智能·python·sklearn
数据大魔方20 分钟前
【期货量化入门】股指期货量化入门:IF/IC/IH交易全攻略(TqSdk完整教程)
开发语言·python
sunfove30 分钟前
空间几何的基石:直角、柱、球坐标系的原理与转换详解
人工智能·python·机器学习
<-->30 分钟前
pytorch vs ray
人工智能·pytorch·python