```
class Solution {
public:
int helper(TreeNode * root)
{
if(!root)return 0;
int leftside=0, rightside=0;
TreeNode *l=root, *r=root;
while(l){
leftside++;
l=l->left;
}
while(r){
rightside++; r=r->right;
}
if(rightside==leftside) return pow(2, rightside)-1;
return helper(root->left)+helper(root->right)+1;
}
int countNodes(TreeNode* root) {
return helper(root);
}
```

};