```
class Solution {
public:
int countNodes(TreeNode* root) {
int res = 0;
int lh = 0, rh = 0;
while(root){
if(!lh){
for(TreeNode * p = root->left; p; p = p->left)
++lh;
}
if(!rh){
for(TreeNode * p = root->right; p; p = p->left)
++rh;
}
if(lh == rh){
res += 1<<lh;
root = root->right;
--lh;
rh = 0;
}else{
res += 1<<rh;
root = root->left;
--lh;
rh = 0;
}
}
return res;
}
};
```

**Reference**: https://leetcode.com/discuss/54900/my-ac-c-solution