# 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: TreeNode) -> bool:
        return self.treeHeight(root)>=0
    def treeHeight(self,root):
        if not root:
            return 0
        left = self.treeHeight(root.left)
        right = self.treeHeight(root.right)
        if left>=0 and right>=0 and abs(left-right)<=1:
            return max(left,right)+1
        return -1
        

 

posted on 2020-04-23 11:38  topass123  阅读(119)  评论(0编辑  收藏  举报