Python, LeetCode, 110. 平衡二叉树

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


class Solution:
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root == None:
            return True
        bl = self.treeHeight(root.left)  
        br = self.treeHeight(root.right)
        if abs(bl - br) > 1:
            return False
        else:
            return self.isBalanced(root.left) and self.isBalanced(root.right)   
        
    def treeHeight(self, root):
        if root == None:
            return 0
        return max(self.treeHeight(root.left), self.treeHeight(root.right)) + 1
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/u010342040/article/details/80340434
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞