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
相关推荐
360安全应急响应中心14 分钟前
Python代码保护之重置操作码映射的攻与防探究(一)
python·逆向
码界奇点36 分钟前
Python内置函数全解析:30个核心函数语法、案例与最佳实践指南
linux·服务器·python
dreams_dream1 小时前
django错误记录
后端·python·django
MC皮蛋侠客1 小时前
使用Python实现DLT645-2007智能电表协议
python·网络协议·tcp/ip·能源
中等生2 小时前
Python 的循环引入问题
python
站大爷IP3 小时前
Python字符串全解析:从基础操作到高级技巧
python
中等生3 小时前
FastAPI vs Flask 性能对比:异步的真正优势在哪里?
python
DFT计算杂谈3 小时前
EPWpy教程:一个脚本完成能带、声子、电声耦合、弛豫时间计算
python
hui函数3 小时前
Flask蓝图:模块化开发的利器
后端·python·flask
跟橙姐学代码3 小时前
Python 装饰器超详细讲解:从“看不懂”到“会使用”,一篇吃透
前端·python·ipython