leetcode-平衡二叉树

110. 平衡二叉树

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        def get_height(root):
            if not root:
                return 0
            left_height = get_height(root.left)
            right_height = get_height(root.right)
            if left_height == -1:
                return -1
            if right_height == -1:
                return -1
            if abs(left_height - right_height) > 1:
                return -1
            else:
                return 1 + max(left_height, right_height)
        if get_height(root) != -1:
            return True
        else:
            return False

相关推荐

  1. leetcode-平衡

    2024-01-16 23:32:03       35 阅读
  2. [leetcode] 110. 平衡

    2024-01-16 23:32:03       12 阅读
  3. LeetCode110. 平衡

    2024-01-16 23:32:03       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-16 23:32:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-16 23:32:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-16 23:32:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-16 23:32:03       20 阅读

热门阅读

  1. 代码随想录day29 回溯开始进入排列问题

    2024-01-16 23:32:03       32 阅读
  2. Python从入门到精通秘籍五

    2024-01-16 23:32:03       39 阅读
  3. c++关键字const

    2024-01-16 23:32:03       32 阅读
  4. 如何在 Edge 浏览器中设置自动刷新?

    2024-01-16 23:32:03       69 阅读
  5. Edge 浏览器设置自动刷新

    2024-01-16 23:32:03       32 阅读
  6. nginx使用入门的笔记

    2024-01-16 23:32:03       33 阅读
  7. C++中的23种设计模式精讲

    2024-01-16 23:32:03       30 阅读
  8. Mock.js使用并且添加到数据库中

    2024-01-16 23:32:03       40 阅读
  9. LeeCode前端算法基础100题(17)- 罗马数字转整数

    2024-01-16 23:32:03       32 阅读