平衡二叉树
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
#递归方法
#通过比较当前节点为根的二叉树高度,来判断是否为平衡二叉树,此时可用后序遍历
#递归三部曲:确定函数参数和返回值;明确中止条件;确定单层递归的逻辑
def height(root:TreeNode)->int:
if not root:
return 0
return max(height(root.left),height(root.right))+1
if not root:
return True
return abs(height(root.left)-height(root.right))<=1 and self.isBalanced(root.left) and self.isBalanced(root.right)