```
class Solution {
public:
bool isBalanced(TreeNode *root) {
vector<int> v;
_depth(root, 1, v);
if (v.size() > 0) {
if (v.size() == 1)
return v[0] - 1 > 1 ? false : true;
else {
int max = INT_MIN;
int min = INT_MAX;
for (int i = 0; i < v.size(); i++) {
max = v[i] > max ? v[i] : max;
min = v[i] < min ? v[i] : min;
}
return max - min > 1 ? false : true;
}
}
return true;
}
void _depth(TreeNode *p, int depth, vector<int> &v) {
if (p) {
if (!p->left && !p->right) {
v.push_back(depth);
}
_depth(p->left, depth+1, v);
_depth(p->right, depth+1, v);
}
}
};
```