树的实现(2)

# 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
        
原文地址:https://www.cnblogs.com/topass123/p/12759655.html