描述
Given a binary tree, determine if it is height-balanced.
判断一个二叉树是否为平衡二叉树
例子
思路
递归
对于每一个结点
为空时:是平衡结点
不为空时:当左右子树皆为平衡二叉树,且高度差小于2时,该结点为平衡二叉树
答案
- python
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
if not root:
return True
if self.isBalanced(root.left) and self.isBalanced(root.right) and abs