bool isBalanced(){
bool balance = true;
isBalanced(root, balance);
return balance;
}
int isBalanced(Node* root, bool& balance){
if (root == NULL) return 0;
int left_height = 1 + height(root->left);
int right_height = 1 + height(root->right);
if (std::abs(left_height - right_height) > 1){
balance = false;
}
return left_height > right_height ? left_height : right_height;
}
如果树不平衡,我希望 isBalanced() 将平衡设置为 false,但是当我运行它时,它无法正确检查树是否平衡,有人可以帮助我吗?谢谢