之前没有通过的样例
return语句只写了一个
return abs(l-r)<=1
缺少了
isBalanced(root->left)&&isBalanced(root->right);
补上就好了
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(!root){
return true;
}
int l=getHeight(root->left);
int r=getHeight(root->right);
return abs(l-r)<=1&&isBalanced(root->left)&&isBalanced(root->right);
}
int getHeight(TreeNode* node){
if(!node){
return 0;
}
int l=1+getHeight(node->left);
int r=1+getHeight(node->right);
return max(l,r);
}
};