class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root == nullptr)
return true;
// 先判断根节点,再递归判断左右子树
return balanced(root) && isBalanced(root->left) && isBalanced(root->right);
}
int balanced(TreeNode* root){
if(root == nullptr)
return 0;
int left = balanced(root->left);
int right = balanced(root->right);
return abs(left-right)>1 ? 0 : max(left,right)+1;
}
};