# 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
本文来自博客园,作者:topass123,转载请注明原文链接:https://www.cnblogs.com/topass123/p/12759655.html