Always check the depth of the right sub tree. If the depth equals that of left sub tree, the nodes count of left sub tree would be clear.

The depth of left sub tree will always be known by node's father, so this value can be passed recursively.

public class Solution {

```
public int countNodes(TreeNode root) {
TreeNode temp = root;
int height = -1;
while(temp != null) {
height++;
temp = temp.left;
}
return count(root, height);
}
public int count(TreeNode node, int depth) {
if(node == null) {
return 0;
}
TreeNode temp = node.right;
int rightHeight = 0;
while(temp != null) {
rightHeight++;
temp = temp.left;
}
if(rightHeight == depth) {
return (1 << depth) + count(node.right, rightHeight - 1);
} else {
return (1 << (depth - 1)) + count(node.left, depth - 1);
}
}
```

}