```
int countNodes(TreeNode* root) {
if(!root) return 0;
TreeNode* p = root;
int height = 0;
int result = 0;
while(p->left) {height++; p = p->left;}
long power = pow(2,height);
int depth = 0;
while (root->left) {
if(!root->right) break;
else {
depth++;
power /= 2;
p = root->right;
int count = depth;
while(p->left) {p=p->left;count++;}
if (count==height) {
root = root->right;
result += power;
} else {
root = root->left;
}
}
}
return result+pow(2, height);
}
```