```
// returns height of the subtree if it's balanced; 0 otherwise
int getBalancedHeight(TreeNode* root) {
if (!root) return 1;
int left_height = getBalancedHeight(root->left);
if (left_height == 0) return 0;
int right_height = getBalancedHeight(root->right);
if (right_height == 0) return 0;
int diff = abs(left_height - right_height);
if (diff > 1) return 0;
else return (1 + max(left_height, right_height));
}
bool isBalanced(TreeNode* root) {
return getBalancedHeight(root) == 0 ? false : true;
}
```