110.Balanced Binary Tree

 

 

# 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: TreeNode) -> bool:
        if root == None:
            return True
        l_depth, r_depth = 0 , 0
        if root.left != None:
            l_depth = self.subtreeHight(root.left)
        if root.right != None:
            r_depth = self.subtreeHight(root.right)
        if abs(l_depth - r_depth) > 1:
            return False
        else:
            return self.isBalanced(root.left) and self.isBalanced(root.right)
        
    def subtreeHight(self, root):
        if root == None:
            return 0
        return max(self.subtreeHight(root.left), self.subtreeHight(root.right)) + 1

 

posted @ 2020-05-09 14:35  星海寻梦233  阅读(85)  评论(0编辑  收藏  举报