```
class Solution {
public:
bool isBalanced(TreeNode *root) {
int depth = 0;
return helper(root, depth);
}
bool helper(TreeNode* root, int &depth) {
if (root == NULL) return true;
int l = depth+1;
int r = depth+1;
if (!helper(root->left, l) || !helper(root->right, r) || abs(l-r) > 1) {
return false;
}
depth = max(l,r);
return true;
}
};
```