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
相关推荐
毛飞龙3 小时前
Python类(class)参数self的理解
python··self
魔尔助理顾问3 小时前
系统整理Python的循环语句和常用方法
开发语言·后端·python
薰衣草23333 小时前
一天两道力扣(6)
算法·leetcode
逝雪Yuki3 小时前
Leetcode——287. 寻找重复数
c++·leetcode·二分查找·双指针·环形链表
科大饭桶4 小时前
数据结构自学Day13 -- 快速排序--“前后指针法”
数据结构·算法·leetcode·排序算法·c
颜颜yan_4 小时前
Python面向对象编程详解:从零开始掌握类的声明与使用
开发语言·redis·python
我的ID配享太庙呀6 小时前
Django 科普介绍:从入门到了解其核心魅力
数据库·后端·python·mysql·django·sqlite
@蓝莓果粒茶7 小时前
LeetCode第350题_两个数组的交集II
c++·python·学习·算法·leetcode·职场和发展·c#
设计师小聂!7 小时前
力扣热题100----------53最大子数组和
java·数据结构·算法·leetcode
FinAnalyzer7 小时前
如何在 InsCodeAI 上搭建并使用 Jupyter Notebook 环境?
ide·python·jupyter