The following solution was I learned from the discussion trying to refine my code, however, I don't understand why it runs much slower than the common O(N^2) solution, which recursively calls depth and isBalanced. Thank you so much!

class Solution {

public:

```
int height(TreeNode* root){
if(root == NULL){
return 0;
}
int left = height(root -> left);
if(left == -1){
return -1;
}
int right = height(root -> right);
if(right == -1){
return -1;
}
if(abs(left - right) > 1){
return -1;
}
return max(height(root -> left), height(root -> right)) + 1;
}
bool isBalanced(TreeNode* root) {
return height(root) != -1;
}
```

};