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
相关推荐
鸿业远图科技14 分钟前
分式注记种表达方式arcgis
python·arcgis
别让别人觉得你做不到1 小时前
Python(1) 做一个随机数的游戏
python
小彭律师2 小时前
人脸识别门禁系统技术文档
python
爱coding的橙子3 小时前
每日算法刷题Day2 5.10:leetcode数组1道题3种解法,用时40min
算法·leetcode
阳洞洞4 小时前
leetcode 18. 四数之和
leetcode·双指针
张小九994 小时前
PyTorch的dataloader制作自定义数据集
人工智能·pytorch·python
zstar-_4 小时前
FreeTex v0.2.0:功能升级/支持Mac
人工智能·python·macos·llm
苏生要努力4 小时前
第九届御网杯网络安全大赛初赛WP
linux·python·网络安全
Kidddddult4 小时前
力扣刷题Day 48:盛最多水的容器(283)
算法·leetcode·力扣
于壮士hoho4 小时前
DeepSeek | AI需求分析
人工智能·python·ai·需求分析·dash